线程“main”中的异常org.hibernate.MappingException:关联引用未映射的类hibernate

时间:2013-01-03 14:17:12

标签: java hibernate java-ee exception hibernate-mapping

我有一个hibernate异常,这是日志和异常。 我将附上代码和hbm地图。

12:39:45,077  INFO Environment:570 - Hibernate 3.5.6-Final
12:39:45,124  INFO Environment:588 - loaded properties from resourcehibernate.properties: {hibernate.bytecode.use_reflection_optimizer=false}
12:39:45,124  INFO Environment:781 - Bytecode provider name : javassist
12:39:45,124  INFO Environment:662 - using JDK 1.4 java.sql.Timestamp handling
12:39:45,483  INFO Configuration:1518 - configuring from resource: /hibernate.cfg.xml
12:39:45,483  INFO Configuration:1495 - Configuration resource: /hibernate.cfg.xml
12:39:45,686  INFO Configuration:655 - Reading mappings from resource : com/beans /User.hbm.xml
12:39:45,874  INFO HbmBinder:348 - Mapping class: User -> user
12:39:46,092  INFO Configuration:655 - Reading mappings from resource : com/beans/Groupe.hbm.xml
12:39:46,139  INFO HbmBinder:348 - Mapping class: Groupe -> groupe
12:39:46,155  INFO Configuration:1633 - Configured SessionFactory: sf
Exception in thread "main" org.hibernate.MappingException: Association references unmapped class: com.beans.User
at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2473)
at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2752)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:66)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1221)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
at com.beans.Logic.init(Logic.java:22)
at com.beans.Logic.<init>(Logic.java:16)
at com.beans.Test.main(Test.java:12)

代码是用于会话管理的Logic.java:

package com.beans;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.*;

public class Logic {
protected Configuration cfg;
protected SessionFactory sfg;
protected Session s;
protected Transaction tx;

public Logic() {

    this.init();

}

public void init() {
    this.setCfg(new Configuration().configure());
    this.setSfg(this.getCfg().buildSessionFactory());
    this.setS(this.getSfg().openSession());
    this.setTx(this.getS().beginTransaction());
}

public Configuration getCfg() {
    return cfg;
}

public void setCfg(Configuration cfg) {
    this.cfg = cfg;
}

public SessionFactory getSfg() {
    return sfg;
}

public void setSfg(SessionFactory sfg) {
    this.sfg = sfg;
}

public Session getS() {
    return s;
}

public void setS(Session s) {
    this.s = s;
}

public Transaction getTx() {
    return tx;
}

public void setTx(Transaction tx) {
    this.tx = tx;
}

}

用户类:

 package com.beans;

 // Generated 3 janv. 2013 12:07:19 by Hibernate Tools 3.4.0.CR1

/**
* User generated by hbm2java
 */
  public class User implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private Integer userId;
private Groupe groupe;
private String username;
private String password;
private String email;

public User() {
}

public User(Groupe groupe, String username, String password, String email) {
    this.groupe = groupe;
    this.username = username;
    this.password = password;
    this.email = email;
}

public Integer getUserId() {
    return this.userId;
}

public void setUserId(Integer userId) {
    this.userId = userId;
}

public Groupe getGroupe() {
    return this.groupe;
}

public void setGroupe(Groupe groupe) {
    this.groupe = groupe;
}

public String getUsername() {
    return this.username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

 }

user.hbm:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 3 janv. 2013 12:07:19 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="User" table="user" catalog="apurement">
    <id name="userId" type="java.lang.Integer">
        <column name="user_id" />
        <generator class="identity" />
    </id>
    <many-to-one name="groupe" class="com.beans.Groupe" fetch="select">
        <column name="groupe_id" not-null="true" />
    </many-to-one>
    <property name="username" type="string">
        <column name="username" length="45" not-null="true" />
    </property>
    <property name="password" type="string">
        <column name="password" length="45" not-null="true" />
    </property>
    <property name="email" type="string">
        <column name="email" length="45" not-null="true" />

    </property>

</class>
 </hibernate-mapping>

groupe.java:

package com.beans;

// default package
// Generated 3 janv. 2013 12:07:19 by Hibernate Tools 3.4.0.CR1

import java.util.HashSet;
import java.util.Set;

/**
 * Groupe generated by hbm2java
 */
 public class Groupe implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private Integer groupeId;
private String groupeName;
private String groupeRole;
private Set users = new HashSet(0);

public Groupe() {
}

public Groupe(String groupeName, String groupeRole) {
    this.groupeName = groupeName;
    this.groupeRole = groupeRole;
}

public Groupe(String groupeName, String groupeRole, Set users) {
    this.groupeName = groupeName;
    this.groupeRole = groupeRole;
    this.users = users;
}

public Integer getGroupeId() {
    return this.groupeId;
}

public void setGroupeId(Integer groupeId) {
    this.groupeId = groupeId;
}

public String getGroupeName() {
    return this.groupeName;
}

public void setGroupeName(String groupeName) {
    this.groupeName = groupeName;
}

public String getGroupeRole() {
    return this.groupeRole;
}

public void setGroupeRole(String groupeRole) {
    this.groupeRole = groupeRole;
}

public Set getUsers() {
    return this.users;
}

public void setUsers(Set users) {
    this.users = users;
}

}

Groupe.hbm.xml

<?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 <!-- Generated 3 janv. 2013 12:07:19 by Hibernate Tools 3.4.0.CR1 -->
 <hibernate-mapping>
   <class name="Groupe" table="groupe" catalog="apurement">
      <id name="groupeId" type="java.lang.Integer">
        <column name="groupe_id" />
        <generator class="identity" />
      </id>
     <property name="groupeName" type="string">
        <column name="groupe_name" length="100" not-null="true" />
    </property>
    <property name="groupeRole" type="string">
        <column name="groupe_role" length="45" not-null="true" />
    </property>
    <set name="users" table="user" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="groupe_id" not-null="true" />
        </key>
        <one-to-many class="com.beans.User" />
    </set>
  </class>
 </hibernate-mapping>

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 name="sf">
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/apurement</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <mapping class="com.beans.User" resource="com/beans/User.hbm.xml"/>
  <mapping class="com.beans.Groupe" resource="com/beans/Groupe.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

好的follks,问题在哪里?

谢谢

1 个答案:

答案 0 :(得分:1)

<class name="User" table="user" catalog="apurement">应为<class name="com.beans.User" table="user" catalog="apurement">

简而言之:提供一个完全限定的类名。

您需要提供完全限定的类名,不仅要提供声明,还要提供引用类的任何位置。喜欢多对一或关系

<many-to-one name="cuentas" class="com.banking.domain.Cuenta" fetch="select">
        <column name="cuenta_id" />
    </many-to-one>

<one-to-many class="com.mauriciogracia.banking.domain.Cuenta" />