@ManyToOne和@OneToOne与@EmbeddedId的关系
技术问答
295 人阅读
|
0 人回复
|
2023-09-12
|
我试图实现数据库实体ID从单个long包括两个long的复合ID,这些ID下面显示的封装ID.class中。您将对ManyToOne和OneToMany关系用什么注释?我的注释(尤其是@
" p, ~& U& h8 DJoinColumns,@ Embeddable和@EmbeddedId)你犯了错误吗?我的问题是存储关系null。
% ~$ I7 J( u3 Y: k4 w我的ID包括自动生成的类别long(pid)手动辅助设置long(sid)。这是我所有实体类的ID:
: g. `" }& Z/ `% Q T@Embeddablepublic class ID implements Serializable @GeneratedValue(strategy = GenerationType.SEQUENCE) private long pid; @Column(nullable=false) private long sid;}我的实体类(示例)和Office与设置有多对一关系(两种关系都是单向的)。3 V: `& C+ g. A( J' T- n
@Entity@Table(name="user")public class User @EmbeddedId private ID id; @ManyToOne(fetch=FetchType.LAZY) @JoinColumns((((((( @JoinColumn(name = "office_pid",referencedColumnName = "pid",updatable=false,insertable=false), @JoinColumn(name = "office_sid",referencedColumnName = "sid",updatable=false,insertable=false) private Office office; @OneToOne(cascade={CascadeType.PERSIST,CascadeType.REMOVE},fetch=FetchType.LAZY) @JoinColumns((((((( @JoinColumn(name = "setting_pid",referencedColumnName = "pid",updatable=false,insertable=false), @JoinColumn(name = "setting_sid",referencedColumnName = "sid",updatable=false,insertable=false) private Settings setting; @OneToMany(mappedBy = "user",cascade=CascadeType.REMOVE,fetch=FetchType.LAZY) private List accounts= new ArrayList();}Office类(多对一示例):# w$ x/ R8 u- v& }3 r4 a
@Entity@Table(name = "office")public class Office @EmbeddedId private ID id; @Column(length = private String description;}账户类别(一对多示例):& Q2 I2 f s- o" w/ i& X
@Entity@Table(name="account")public class Account @EmbeddedId private ID id; @Column(length = 16) private String description; @ManyToOne(fetch=FetchType.LAZY) @JoinColumns((((((( @JoinColumn(name = "user_pid",referencedColumnName = "pid"), @JoinColumn(name = "user_sid",referencedColumnName = "sid") private User user;}感谢您的帮助。
, z6 {3 R. }: Q! t/ D
: k4 _+ q& z4 q7 b 解决方案: |
|
|
|
|
|