@OneToOne注释问题(我认为......)
我是 hibernate 和 JPA 的新手,我在注释方面遇到了一些问题。
我的目标是在数据库中创建此表(带有个人详细信息的PERSON_TABLE)
ID ADDRESS NAME SURNAME MUNICIPALITY_ID
首先,我在数据库中有一个包含我国所有城市的市政表。 我将这个表映射到这个实体中:
@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
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() {
}
...
当我必须保存新的个人记录时,一切都很好,事实上 hibernate 创建我想要的 PERSON_TABLE ,但是如果我尝试检索 Person 记录,则会出现异常。 HQL 简单来说就是“来自 Person” 例外是(Entities 是包含上述所有类的包):
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>
谢谢。
im new in hibernate and JPA and i have some problems with annotations.
My target is to create this table in db (PERSON_TABLE with personal-details)
ID ADDRESS NAME SURNAME MUNICIPALITY_ID
First of all, i have a MUNICIPALITY table in db containing all municipality of my country.
I mapped this table in this 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() {
}
...
Then i make an EMBEDDABLE class Address containing fields that realize a simple address...
@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() {
}
...
Finally i embedded this class into Person 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() {
}
...
All works good when i have to save a new Person record, in fact hibernate creates a PERSON_TABLE as i want, but if i try to retrieve a Person record i have an exception.
HQL is simply "from Person"
The excpetion is (Entities is the package containing all classes above-mentioned):
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on Entities.Person.address.municipality references an unknown entity: Entities.Municipality
Is the @OneToOne annotation the problem?
My hibernate.cfg.xml is this:
<?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>
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是...我将查询放入 netbeans“HQL Query()”选项卡中,我尝试创建一个查询
,一切正常。
抱歉各位,我的错,我首先进行此测试...我认为 Netbeans 中的 HQL 向导是测试查询的可靠工具...
在这种情况下,我必须选择哪个答案?一切都合理且充满技巧!
再次抱歉。
The problem is... i lunch my querys into netbeans "HQL Query()" tab, i try to create a query
and all works good.
Sorry guys my fault, i do this test first... i thought that HQL wizard into netbeans was a reliable tool to test queries...
In this case, wich of answer i have to choice? All are reasonable and full of tips!
Sorry again.
我想您的错误只是因为您将 @Embeddable 类(地址)定义为实体。 @Embeddable 类不是一个普通实体,因此将其从 hibernate.cfg.xml 文件中删除
I suppose your error is just because you have defined a @Embeddable class (Address) as Entity. @Embeddable class is not a plain Entity, so remove it from your hibernate.cfg.xml file
不。问题在于 Hibernate 不将
Municipality
识别为实体。它配置正确吗(例如在你的 persistence.xml 中)?编辑:对于这种情况,错误消息很奇怪,我不知道这是否真的是问题所在,但
Address.municipality
应该定义为@ ManyToOne
(因为会有多个地址引用同一个城市)。No. The problem is that Hibernate does not recognize
Municipality
as an entity. Is it configured properly (e.g. in your persistence.xml)?Edit: The error message is weird for this situation, and I don't know if that is really the problem, but
Address.municipality
should be defined as@ManyToOne
(as there will be multiple addresses referencing the same municipality).你确定你使用的是
@javax.persistence.Entity
而不是@org.hibernate.annotations.Entity
吗?好吧,另一个猜测。您可以尝试将
@OneToOne
更改为@OneToOne(targetEntity = Municipality.class)
吗?Are you damn sure you use
@javax.persistence.Entity
instead of@org.hibernate.annotations.Entity
?OK, another guess. Could you try changing the
@OneToOne
to@OneToOne(targetEntity = Municipality.class)
?