【问题标题】:Hibernate ForeignKey mapping annotationsHibernate ForeignKey 映射注解
【发布时间】:2016-12-15 09:10:31
【问题描述】:

我想让休眠生成一些带有外键的表等等。我会给你一个我希望hibernate生成的查询的例子:

create table RealtimeCost(id INTEGER not null primary key Autoincrement,
    mnemonic varchar(50)not null references Exchange(mnemonic),
    sid int not null references License(sid),
    price numeric(10,2) not null)

所以这个查询应该由 hibernate 通过 Annotations 生成。对应的类是:

@Entity
@Table
public class RealtimeCost {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @MapsId("mnemonic")
    @JoinColumn(referencedColumnName="sid")
    private String mnemonic;
    @MapsId("sid")
    @JoinColumn(referencedColumnName="sid")
    private Integer sid;
    @Column
    private Double price;

RealtimeCost 中的 mnemonic 应映射到的示例(RealtimeCost 中的每个助记符在 Exchange 中恰好有 1 个值):

@Entity
@Table
public class Exchange {
    @Id
    @Column(name="mnemonic")
    private String exchange;
    @Column
    private String description;

如您所见,我在文档的帮助下进行了一些尝试,但我无法让 hibernate 生成外键。如果有人能告诉我这个类所需的注释和值,那就太好了,这样我也可以自己为其他类做。另外请告诉我是否需要更改 Exchange 类中的任何内容以使映射正常工作。提前致谢

【问题讨论】:

    标签: java hibernate hibernate-mapping


    【解决方案1】:
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "accommodation_type", unique = true, nullable = false)
    private AccommodationType accommodationType;
    

    @ManyToOne()根据@JoinColumn()创建关系 @JoinColumn() 中的name 是您要建立连接的表名。

    然后当你创建一个要连接到主类的类时,你首先需要在@Entity下面给它一个表名,例如@Table(name="accommodation_types")

    然后你创建你的变量。

    //bi-directional many-to-one association to Accommodation
    @OneToMany(mappedBy="accommodationType", fetch=FetchType.EAGER)
    private List<Accommodation> accommodations;
    

    mappedBy的值是主类中的变量名。

    【讨论】:

      【解决方案2】:

      我不是专家,但我们让 hibernate 使用 javax.persistence 注释完成所有工作以连接实体。

        @javax.persistence.ManyToOne( fetch = javax.persistence.FetchType.EAGER, optional = true )
        @javax.persistence.JoinColumn( name = "VIEWTYPE_ID", nullable = true, unique = false, insertable = true, updatable = true )
        private com.company.other.subproject.ViewType viewType;
      

      也许这就是你需要的。既然这样,让我们​​ hibernate 关心是否必须创建表,并且使用您与之通信的数据库的方言自动创建 foreignKeys。

      【讨论】:

      • 这是否将ViewType 类型的对象仅映射到ViewType 类的ID?
      • 你能告诉我你是如何注释 viewtype id 的吗?
      • 抱歉回复晚了。是的,它将实体映射为 ID 参考。使用 FetchType,您可以进一步指定它应该如何加载。 (懒惰或急切) ViewType 使用此 @AttributeOverrides( { @AttributeOverride( name = "id", column = @Column( name = "VIEWTYPE_ID" ) ) } ) 进行注释,但此注释位于类定义中。因为我们使用了另一种声明实体的策略。但是 id 列是用@Id 注释的。这是 fetchtype @javax.persistence.Basic( fetch = javax.persistence.FetchType.EAGER, optional = false ) 的示例注释
      【解决方案3】:

      您应该在一个实体中设置关联并在另一个实体中使用mappedBy。您不需要@MapsId,因为您没有使用嵌入式实体(阅读文档)。看看@OneToMany@ManyToOne 关系:

      @Entity
      @Table
      public class RealtimeCost {
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          private Integer id;
      
          @OneToMany
          @JoinColumn(name="mnemonic")
          private Exchange exchange;
          ...
      }
      
      @Entity
      @Table
      public class Exchange {
          @Id
          @Column(name="mnemonic")
          private String mnemonic;
      
          @Column
          private String description;
      
          @ManyToOne(mappedBy="exchange")
          private RealtimeCost realtimeCost;
          ...
      }
      

      【讨论】:

      • 这个@ManyToOne(mappedBy="exchange") 给我带来了错误The attribute mappedBy is undefined for the annotation type ManyToOne
      【解决方案4】:

      这里发布的每个答案都得到了我的支持,因为每个人都有点正确,但这并不是我所寻找的 100%,但它帮助我自己解决了我的问题。对于我发布的示例,我正在寻求的解决方案如下(我还添加了不可为空):

      @Entity
      @Table
      public class RealtimeCost {
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          private Integer id;
          @ManyToOne
          @JoinColumn(name = "mnemonic",nullable=false)
          private Exchange exchange;
          @ManyToOne
          @JoinColumn(name = "sid",nullable=false)
          private License license;
          @Column(nullable=false)
          private Double price;
      

      这些是我为RealtimeCost 类寻找的注释。我不需要 Exchange 类中的任何特殊注释。 @Nico 的回答最接近我的需要,因此他的回答将被接受

      【讨论】:

        猜你喜欢
        • 2015-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-02
        • 2014-01-22
        • 2012-03-22
        • 1970-01-01
        相关资源
        最近更新 更多