猿问

JPA休眠一对一关系

我有一对一的关系,但是hibernatetool在生成模式时抱怨。这是显示问题的示例:


@Entity

public class Person {

    @Id

    public int id;


    @OneToOne

    public OtherInfo otherInfo;


    rest of attributes ...

}

人与OtherInfo具有一对一关系:


@Entity

public class OtherInfo {

    @Id

    @OneToOne(mappedBy="otherInfo")

    public Person person;


    rest of attributes ...

}

人是OtherInfo的拥有方。OtherInfo是拥有方,因此person用于mappedBy在Person中指定属性名称“ otherInfo”。


使用hibernatetool生成数据库架构时出现以下错误:


org.hibernate.MappingException: Could not determine type for: Person, at table: OtherInfo, for columns: [org.hibernate.mapping.Column(person)]

        at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:292)

        at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:175)

        at org.hibernate.cfg.Configuration.iterateGenerators(Configuration.java:743)

        at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:854)

        at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:128)

        ...

知道为什么吗?我是在做错什么还是这是Hibernate错误?


MM们
浏览 387回答 3
3回答

开满天机

使用JPA 2.0 @MapsId注释而不是Hibernate的GenericGenerator,这也应该起作用:@Entitypublic class Person {&nbsp; &nbsp; @Id&nbsp; &nbsp; @GeneratedValue&nbsp; &nbsp; public int id;&nbsp; &nbsp; @OneToOne&nbsp; &nbsp; @PrimaryKeyJoinColumn&nbsp; &nbsp; public OtherInfo otherInfo;&nbsp; &nbsp; rest of attributes ...}@Entitypublic class OtherInfo {&nbsp; &nbsp; @Id&nbsp; &nbsp; public int id;&nbsp; &nbsp; @MapsId&nbsp; &nbsp; @OneToOne&nbsp; &nbsp; @JoinColumn(name="id")&nbsp; &nbsp; public Person person;&nbsp; &nbsp; rest of attributes ...}有关详细信息,请参见Hibernate 4.1文档中的第5.1.2.2.7节。
随时随地看视频慕课网APP

相关分类

Java
我要回答