没有@EmbeddedId或@IdClass的超级简单组合键 [英] Super simple composite key without @EmbeddedId or @IdClass
问题描述
我不知道,因为这是可能的,但我只是按照我一直想要的方式创建组合键:无 @IdClass
必需!!
- Spring 4.0.0.M3
- Hibernate 4.3.0.Beta4
- JPA 2.1
User.java :
@Entity
public class User {
@Id
..
@OneToMany(的mappedBy = 用户,级联= CascadeType.ALL,orphanRemoval =真)
私人列表与LT; GroupUser> groupUsers;
Group.java :
@Entity
public class Group {
@Id
...
@OneToMany(mappedBy =user,cascade = CascadeType.ALL,orphanRemoval = true)
private List< GroupUser> groupUsers;
GroupUser.java :
'pre>
@Entity
公共类GroupUser实现Serializable {
@Id
@ManyToOne
@JoinColumn( name =Group_id)
私人群组;
@Id
@ManyToOne
@JoinColumn(name =User_id)
私人用户用户;
@Id
@Column
private String s;
@Column
private int i;
}
以下是MySQL所说的:
USER GROUP_USER GROUP
------ ---------------------- --- ---
id User_id(PK,NOT NULL)id
Group_id(PK,NOT NULL)
s(PK,NOT NULL)
i(DEFAULT NULL)
GROUP_USER
详情:
- PRIMARY KEY:User_id,Group_id,s
- INDEX#1 / FOREIGN KEY:User_id
- INDEX#2 / FOREIGN KEY:Group_id
I还做了一些其他的测试,例如: 像一个魅力一样! 是否仍然有理由使用 并且:这是JPA 2.1还是Hibernate特性的一部分? I don't know since when this is possible, but I just created composite keys the way I always wanted: No User.java: Group.java: GroupUser.java: And here is what MySQL says: I also did some other tests, like this: Works like a charm!! Is there still any reason to use And: Is this part of JPA 2.1 or an Hibernate feature? JPA requires a primary key class of some sort be defined for composite primary keys. In practice though, this seems required mostly for the EntityManager find and getReference calls that require a pk class instance. Implementations may have their own pk representation that you can use for these calls, but will not be portable. 这篇关于没有@EmbeddedId或@IdClass的超级简单组合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
pre $ @Entity
public class A implements Serializable {
@Id
@Column
String a;
@Id
@Column
字符串b;
@Column
String c;
@EmbeddedId
或 @ IdClass
?
@EmbeddedId
and no @IdClass
required!!
@Entity
public class User {
@Id
...
@OneToMany(mappedBy="user", cascade=CascadeType.ALL, orphanRemoval=true)
private List<GroupUser> groupUsers;
}
@Entity
public class Group {
@Id
...
@OneToMany(mappedBy="user", cascade=CascadeType.ALL, orphanRemoval=true)
private List<GroupUser> groupUsers;
}
@Entity
public class GroupUser implements Serializable {
@Id
@ManyToOne
@JoinColumn(name="Group_id")
private Group group;
@Id
@ManyToOne
@JoinColumn(name="User_id")
private User user;
@Id
@Column
private String s;
@Column
private int i;
}
USER GROUP_USER GROUP
------ ------------------------- -------
id User_id (PK, NOT NULL) id
Group_id (PK, NOT NULL)
s (PK, NOT NULL)
i (DEFAULT NULL)
GROUP_USER
details:
@Entity
public class A implements Serializable {
@Id
@Column
String a;
@Id
@Column
String b;
@Column
String c;
}
@EmbeddedId
or @IdClass
?