论坛首页 入门技术论坛

hibernate复合主键的问题

浏览 2350 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-04-21  
pojo类
package bbb;

/**
* TCode entity.
*
* @author MyEclipse Persistence Tools
*/

public class TCode implements java.io.Serializable {

// Fields

private TCodeId id;
private String mc;
private String dm;
private Byte yn;
private String bz;

// Constructors

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

/** minimal constructor */
public TCode(TCodeId id, String mc, Byte yn) {
this.id = id;
this.mc = mc;
this.yn = yn;
}

/** full constructor */
public TCode(TCodeId id, String mc, String dm, Byte yn, String bz) {
this.id = id;
this.mc = mc;
this.dm = dm;
this.yn = yn;
this.bz = bz;
}

// Property accessors

public TCodeId getId() {
return this.id;
}

public void setId(TCodeId id) {
this.id = id;
}

public String getMc() {
return this.mc;
}

public void setMc(String mc) {
this.mc = mc;
}

public String getDm() {
return this.dm;
}

public void setDm(String dm) {
this.dm = dm;
}

public Byte getYn() {
return this.yn;
}

public void setYn(Byte yn) {
this.yn = yn;
}

public String getBz() {
return this.bz;
}

public void setBz(String bz) {
this.bz = bz;
}

}


复合id类

package bbb;

/**
* TCodeId entity.
*
* @author MyEclipse Persistence Tools
*/

public class TCodeId implements java.io.Serializable {

// Fields

private String tid;
private String id;

// Constructors

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

/** full constructor */
public TCodeId(String tid, String id) {
this.tid = tid;
this.id = id;
}

// Property accessors

public String getTid() {
return this.tid;
}

public void setTid(String tid) {
this.tid = tid;
}

public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof TCodeId))
return false;
TCodeId castOther = (TCodeId) other;

return ((this.getTid() == castOther.getTid()) || (this.getTid() != null
&& castOther.getTid() != null && this.getTid().equals(
castOther.getTid())))
&& ((this.getId() == castOther.getId()) || (this.getId() != null
&& castOther.getId() != null && this.getId().equals(
castOther.getId())));
}

public int hashCode() {
int result = 17;

result = 37 * result
+ (getTid() == null ? 0 : this.getTid().hashCode());
result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());
return result;
}

}


测试类


package ccc;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import bbb.TCode;
import bbb.TCodeId;


public class HibernateTest {

/**
* @param args  
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
    try {
SessionFactory sessionFactory=null;
Session session=null;
Transaction tx = null;
sessionFactory = new Configuration().configure().buildSessionFactory();

session=sessionFactory.openSession();
tx=session.beginTransaction();
TCode code = (TCode) session.load(bbb.TCode.class, tcodeid);
System.out.println(code.getYn());
tx.commit();
session.close();
sessionFactory.close();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ok");
}

}


出错信息:


org.hibernate.MappingException: Unknown entity: bbb.TCode
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:514)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:66)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:781)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:774)
at ccc.HibernateTest.main(HibernateTest.java:54)

我在网上看到的测试方法,可抱错是这样,查了好久都无法解决!!!


当用这种测试方法时:
                          。。。。。。。。。。。。。。
                    Query query = session.createQuery("select ttt.bz from bbb.TCode as ttt");
List list = query.list();
System.out.println(list.size());
for(int i= 0;i<list.size();i++){
String bz = (String) list.get(i);
System.out.println(bz);
                                   }
   。。。。。。。。。。。。
  怎么list。size ()的值为零?谁解释一下?
             





   发表时间:2008-04-21  
看你的代码很不爽,既没使用工具格式化,类型、变量命名又不遵行Java编程习惯,又弄些魔法名称(bbb,ttt)

看了好久,感觉是类TCode的Hibernate相关配置不正确,Hibernate没有为把该类当成一个实体
0 请登录后投票
   发表时间:2008-04-21  
movingboy 写道
看你的代码很不爽,既没使用工具格式化,类型、变量命名又不遵行Java编程习惯,又弄些魔法名称(bbb,ttt)

看了好久,感觉是类TCode的Hibernate相关配置不正确,Hibernate没有为把该类当成一个实体

哈哈,不好意思.
bbb是包名.
其他的变量名都是hibernate自动生成的!
网上找了好久都是影射的错误.可感觉影射都对啊!
我用下面的查询,数据库里有值但取不到啊?


0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics