MappingException的Unknown entity class错误

boy1 2006-01-09 10:11:42
做的是一个联合主键的例子,不知道和这个有关没有。
net.sf.hibernate.MappingException: Unknown entity class: wyj.pojo.Tuser2
at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:347)
at net.sf.hibernate.impl.SessionImpl.getClassPersister(SessionImpl.java:2710)
at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2717)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:772)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:747)
at wyj.CompositeKey.insert(CompositeKey.java:57)
at wyj.CompositeKey.main(CompositeKey.java:93)
POJO和hbm.xml是用MiddleGen和Extension生成的。wyj.pojo.Tuser2是映射的类
...全文
172 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
boy1 2006-01-11
  • 打赏
  • 举报
回复
package wyj.pojo;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;


/**
* @hibernate.class
* table="t_user_2"
*
*/
public class Tuser2 implements Serializable {

/** identifier field */
private wyj.pojo.Tuser2PK comp_id;

/** nullable persistent field */
private Integer age;

/** full constructor */
public Tuser2(wyj.pojo.Tuser2PK comp_id, Integer age) {
this.comp_id = comp_id;
this.age = age;
}

/** default constructor */
public Tuser2() {
}

/** minimal constructor */
public Tuser2(wyj.pojo.Tuser2PK comp_id) {
this.comp_id = comp_id;
}

/**
* @hibernate.id
* generator-class="assigned"
*
*/
public wyj.pojo.Tuser2PK getComp_id() {
return this.comp_id;
}

public void setComp_id(wyj.pojo.Tuser2PK comp_id) {
this.comp_id = comp_id;
}

/**
* @hibernate.property
* column="age"
* length="11"
*
*/
public Integer getAge() {
return this.age;
}

public void setAge(Integer age) {
this.age = age;
}

public String toString() {
return new ToStringBuilder(this)
.append("comp_id", getComp_id())
.toString();
}

public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( !(other instanceof Tuser2) ) return false;
Tuser2 castOther = (Tuser2) other;
return new EqualsBuilder()
.append(this.getComp_id(), castOther.getComp_id())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getComp_id())
.toHashCode();
}

}
boy1 2006-01-11
  • 打赏
  • 举报
回复
package wyj.pojo;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class Tuser2PK implements Serializable {

/** identifier field */
private String firstname;

/** identifier field */
private String lastname;

/** full constructor */
public Tuser2PK(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}

/** default constructor */
public Tuser2PK() {
}

/**
* @hibernate.property
* column="firstname"
*
*/
public String getFirstname() {
return this.firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

/**
* @hibernate.property
* column="lastname"
*
*/
public String getLastname() {
return this.lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String toString() {
return new ToStringBuilder(this)
.append("firstname", getFirstname())
.append("lastname", getLastname())
.toString();
}

public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( !(other instanceof Tuser2PK) ) return false;
Tuser2PK castOther = (Tuser2PK) other;
return new EqualsBuilder()
.append(this.getFirstname(), castOther.getFirstname())
.append(this.getLastname(), castOther.getLastname())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getFirstname())
.append(getLastname())
.toHashCode();
}

}
boy1 2006-01-11
  • 打赏
  • 举报
回复
Tuser2.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1

http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->

<class
name="wyj.pojo.Tuser2"
table="t_user_2"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="t_user_2"
</meta>

<composite-id name="comp_id" class="wyj.pojo.Tuser2PK">
<meta attribute="field-description" inherit="false">
@hibernate.id
generator-class="assigned"
</meta>
<key-property
name="firstname"
column="firstname"
type="java.lang.String"
length="100"
>
<meta attribute="field-description">
@hibernate.property
column="firstname"
</meta>
</key-property>
<key-property
name="lastname"
column="lastname"
type="java.lang.String"
length="100"
>
<meta attribute="field-description">
@hibernate.property
column="lastname"
</meta>
</key-property>
</composite-id>

<property
name="age"
type="java.lang.Integer"
column="age"
length="11"
>
<meta attribute="field-description">
@hibernate.property
column="age"
length="11"
</meta>
</property>

<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- end of derived association(s) -->


</class>
</hibernate-mapping>
zeq258 2006-01-10
  • 打赏
  • 举报
回复
检查楼上说的这个问题,

如果还不行,把代码铁出来,包括 hibernate.cfg.xml ,和 .hbm.xml
bluelily22 2006-01-10
  • 打赏
  • 举报
回复
hibernate.cfg.xml
有没有把你的类写到mapping 中呀
<mapping resource="wyj/pojp/Tuser2.hbm.xml" />

boy1 2006-01-10
  • 打赏
  • 举报
回复
public void insert(String firstName, String lastName) {

Transaction tran = null;
List emailList = new ArrayList();
try {
Tuser2PK userPK = new Tuser2PK(firstName, lastName);
Tuser2 user = new Tuser2();
user.setComp_id(userPK);
user.setAge(new Integer(25));
session.save(user); //异常
session.flush();

} catch (HibernateException ex) {
ex.printStackTrace();
if(tran != null) {
try {
tran.rollback();
} catch(HibernateException e) {
e.printStackTrace();
}
}
}
}

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧