定义从一个实体到另一个实体的一对多映射的问题

时间:2018-06-08 05:23:10

标签: postgresql jpa java-ee

假设我有2个表T1和T2。

t1id is Primary Key (PK) in table T1 .   
T1 references T2 via t2id    which is Foreign Key(FK)
T1 to T2 is one to many relationship

Schemas : 
--------------------------------------------------------

T1 ( t1id (PK) , name , t2id(FK))
T2 ( t2id(PK) , author)

-------------------------------------------------------

有人可以帮我定义上述要求的enity类吗?

1 个答案:

答案 0 :(得分:0)

您可以编写您的实体类,如下所示

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! TimerTableViewCell

    cell.timer?.invalidate()
    let item = myTimerList[indexPath.row]
    cell.timerName.text = item.timerName
    cell.secondLeftLabel.text = "\(item.timerSecond)"      
    return cell
}

您的T2实体可以写成

@Entity
@Table(name = "T1")
public class T1Entity implements Serializable {

@Id
@Column(name="t1id")
@SequenceGenerator(name = "t1_seq", sequenceName = "t1_seq", allocationSize = 1)
@GeneratedValue(generator = "t1_seq", strategy = GenerationType.SEQUENCE)
private Long t1id;

@ManyToOne(fetch = FetchType.EAGER, targetEntity = T2Entity.class)
@JoinColumn(name = "t2id", referencedColumnName = "t2id")
private T2Entity t2;


//getter and setter    
}