【发布时间】:2013-09-30 18:36:43
【问题描述】:
您好,我有我的实体类 Enterprise 和 Role,代码如下:
@Entity
@Table(name = "enterprises")
public class Enterprise implements Serializable{
@Id
@Column( name = "user_name" )
private String userName;
private String name;
@Column ( name = "tax_id" )
private String taxId;
private String email;
@Column ( name = "contact_name" )
private String contactName;
@Column ( name = "contact_surname" )
private String contactSurname;
private String phone;
@Column ( name = "enabled_account" )
private Boolean enabledAccount;
@ManyToMany(targetEntity = Role.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "enterprise_role", joinColumns = {
@JoinColumn(name = "id_enterprise", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "id_role",
nullable = false, updatable = false) })
private List<Role> roles;
@Column ( name = "enterprise_description" )
private String enterpriseDescription;
private String password;
public Enterprise() {
roles = new ArrayList<Role>();
}
//the getters and setters
还有我的角色类:
@Entity
@Table ( name = "roles" )
public class Role implements Serializable {
@Id
@Column ( name = "id_role" )
private Integer id;
@Column ( name = "role_type" )
private String roleType;
private String description;
public Role() {
}
当我保存一个对象时你没有问题但是当我尝试执行这个查询时: 来自 Enterprise order by user_name desc
得到这个错误:
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.efix.model.Enterprise.roles[com.efix.model.Role]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1068)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:600)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:541)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
我已经在 hibernate.xml 配置文件中定义了这个实体,指向具有整个路径的类,例如 com.example.Enterprise 或 com.example.Role。
任何机构都可以这是为什么?提前致谢
我的 hibernate.cfg 是:
<?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.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/prueba_efix</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">postgres</property>
<mapping class="com.efix.model.Enterprise"/>
<mapping class="com.efix.model.Role"/>
</session-factory>
</hibernate-configuration>
还有我在 HibernateUtil 使用 Netbeans 生成的配置文件。
【问题讨论】:
-
请向我们展示您的
hibernate.xml文件和您的Configuration设置。 -
我编辑帖子并放入hibernate.cfg,HibernateUtil是默认的
-
我相信您可以删除“targetEntity”属性并尝试一下。
-
异常仍然存在,很少是因为我在 .cfg 中定义了实体。另一个建议?
-
你在stackoverflow.com/questions/4956855/…这样的问题中使用了哪个实体注释,由于从 hibarnate 包而不是 javax.persistance 包导入而发生问题
标签: java hibernate exception annotations entity