@OneToOne注释问题(我认为......)

时间:2010-06-02 09:49:01

标签: java hibernate jpa annotations one-to-one

我是hibernate和JPA中的新手,我在注释方面遇到了一些问题。

我的目标是在db中创建此表(PERSON_TABLE包含个人详细信息)

ID      ADDRESS     NAME    SURNAME     MUNICIPALITY_ID

首先,我在db中有一个MUNICIPALITY表,其中包含我所在国家/地区的所有城市。 我在这个ENTITY中映射了这个表:

@Entity
public class Municipality implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String country;
private String province;
private String name;
@Column(name="cod_catasto")
private String codCatastale;
private String cap;


public Municipality() {
}

...

然后我创建一个EMBEDDABLE类地址,其中包含实现简单地址的字段......

@Embeddable
public class Address implements Serializable {

@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="id_municipality")
private Municipality municipality;
@Column(length=45)
private String address;

public Address() {
}

...

最后我将这个课程嵌入了人物ENTITY

@Entity
public class Person implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
private String surname;
@Embedded
private Address address;

public Person() {
}

...

当我必须保存一个新的Person记录时,所有工作都很好,实际上hibernate创建了一个PERSON_TABLE,但是如果我尝试检索一个Person记录,我有一个例外。 HQL简直就是“来自人” 重复是(实体是包含上述所有类的包):

org.hibernate.AnnotationException: @OneToOne or @ManyToOne on Entities.Person.address.municipality references an unknown entity: Entities.Municipality

@OneToOne注释是否存在问题?

我的hibernate.cfg.xml是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
        <property name="hibernate.connection.driver_class">org.h2.Driver</property>
        <property name="hibernate.connection.url">jdbc:h2:tcp://localhost/DB_PATH</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping class="Entities.Person"/>
        <mapping class="Entities.Municipality"/>
        <mapping class="Entities.Address"/>
    </session-factory>
</hibernate-configuration>

感谢。

4 个答案:

答案 0 :(得分:3)

问题是......我在查询netbeans“HQL Query()”选项卡时,我尝试创建查询

session.createQuery("from Person");

并且一切正常。

对不起伙计我的错,我先做了这个测试......我认为HQL向导进入netbeans是测试查询的可靠工具......

在这种情况下,我必须选择回答?一切都很合理,充满了技巧!

再次抱歉。

答案 1 :(得分:2)

我认为您的错误只是因为您已将@Embeddable类(地址)定义为实体。 @Embeddable类不是简单的实体,因此将其从hibernate.cfg.xml文件中删除

<mapping class="Entities.Person"/>
<mapping class="Entities.Municipality"/>

答案 2 :(得分:1)

没有。问题是Hibernate不会将Municipality识别为实体。是否正确配置(例如在persistence.xml中)?

编辑:对于这种情况,错误消息很奇怪,我不知道这是否真的是问题,但Address.municipality应定义为@ManyToOne(因为会有多个地址引用同一个市镇。)

答案 3 :(得分:1)

你确定使用@javax.persistence.Entity代替@org.hibernate.annotations.Entity吗?

好的,另一个猜测。您可以尝试将@OneToOne更改为@OneToOne(targetEntity = Municipality.class)吗?