简体   繁体   English

没有@EmbeddedId或@IdClass的超级简单复合键

[英]Super simple composite key without @EmbeddedId or @IdClass

I don't know since when this is possible, but I just created composite keys the way I always wanted: No @EmbeddedId and no @IdClass required!! 我不知道什么时候开始,但是我只是按照我一直想要的方式创建了复合键:不需要@EmbeddedId@IdClass

  • Spring 4.0.0.M3 春季4.0.0.M3
  • Hibernate 4.3.0.Beta4 休眠4.3.0.Beta4
  • JPA 2.1 JPA 2.1

User.java : User.java

@Entity
public class User {
    @Id
    ...

    @OneToMany(mappedBy="user", cascade=CascadeType.ALL, orphanRemoval=true)
    private List<GroupUser> groupUsers;     
}

Group.java : Group.java

@Entity
public class Group {
    @Id
    ...

    @OneToMany(mappedBy="user", cascade=CascadeType.ALL, orphanRemoval=true)
    private List<GroupUser> groupUsers;     
}

GroupUser.java : GroupUser.java

@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;
}

And here is what MySQL says: 这是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 details: GROUP_USER详细信息:

  • PRIMARY KEY: User_id, Group_id, s 主键:User_id,Group_id,s
  • INDEX #1 / FOREIGN KEY: User_id 索引#1 /外键:User_id
  • INDEX #2 / FOREIGN KEY: Group_id 索引2 /外键:Group_id

I also did some other tests, like this: 我还做了其他一些测试,例如:

@Entity
public class A implements Serializable {

    @Id
    @Column
    String a;

    @Id
    @Column
    String b;

    @Column
    String c;

}

Works like a charm!! 奇迹般有效!!


Is there still any reason to use @EmbeddedId or @IdClass ? 仍然有任何理由使用@EmbeddedId@IdClass吗?

And: Is this part of JPA 2.1 or an Hibernate feature? 并且:这是JPA 2.1的一部分还是Hibernate功能?

JPA requires a primary key class of some sort be defined for composite primary keys. JPA要求为组合主键定义某种主键类。 In practice though, this seems required mostly for the EntityManager find and getReference calls that require a pk class instance. 但是实际上,这似乎是大多数需要pk类实例的EntityManager find和getReference调用所必需的。 Implementations may have their own pk representation that you can use for these calls, but will not be portable. 实现可能具有自己的pk表示形式,您可以将其用于这些调用,但不能移植。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EclipseLink错误:实体类未指定主键。 它应该定义@ Id,@ EmbeddedId或@IdClass - EclipseLink error: Entity class has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass Spring JPA 复合键:此类未定义 IdClass - Spring JPA Composite key: This class does not define an IdClass 使用IdClass使用3个主键的Spring JPA数据JPA复合 - Spring jpa data jpa composite with 3 primary key using IdClass 复合主键,使用@IdClass - 列“id”不能是 null - Composite Primary key, using @IdClass - Column 'id' cannot be null CrudRepository,无法使用 IdClass 保存具有复合主键的实体 - CrudRepository, cant save entity with a composite primary key using IdClass 使用 IdClass 声明的复合键的用于 deleteById 的 JPA 查询 - JPA Query to deleteById for a Composite Key declared using IdClass Spring JPA - 如何使用复合键(EmbeddedID)保存对象 - Spring JPA - How to Save Object with a Composite Key (EmbeddedID) 使用JPA创建复合主键时如何解决找不到IdClass属性的问题 - How to resolve IdClass Property not found issue when creating composite primary key using JPA 不是使用@IdClass创建和组合ID的托管类型 - Not a managed type creating and composite id with @IdClass 使用 DataJpaTest 时在 Hibernate 上使用 IdClass 的复合键作为复合键 - Composite keys using IdClass on Hibernate for composite keys when using DataJpaTest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM