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 ()的值为零?谁解释一下?
分享到:
相关推荐
复合主键映射 <br>通常将复合主键相关属性,单独抽取出来,建立一个独立的类 * 必须实现序列化接口 * 必须实现equals和hashcode方法 采用标签进行映射,其它属性采用正常映射
hibernate复合主键设置,里面很全面的配置,可以拿到直接用
hibernate复合主键的实例,hibernate复合主键的实例。
NULL 博文链接:https://liuna718-163-com.iteye.com/blog/748089
一个简单的复合主键的做关联类的例子
请更名为 Hibernate复合主键.part2.rar
更名为 Hibernate复合主键.part3.rar
Hibernate复合主键.
hibernate 无主键表映射资源说明: 1. 简单Demo 2. Demo对应的sql语句 3. 对应博客文档
本章讲解Hibernate中对数据库复合主键的支持
如果使用Hibernate开发legacy的数据库应用,对于数据库表中有使用字符串作为主键或者使用复合主键情况,那么对于这些情况的影射档是比较麻烦的。该示例应用演示了两张表ITEM和CATEGORY_ITEM表有主外键关系,并且ITEM...
博文链接:https://balaschen.iteye.com/blog/155127
复合主键映射用起来比普通的增加主键字段要复杂,这里我们就来共同学习Java的Hibernate框架中复合主键映射的创建和使用教程,需要的朋友可以参考下
hibernate3 注释生成复合主键或者嵌入式主键的方法及实例
023 复合主键 关联映射 024 其它 关联映射 025 hibernate 悲观锁、乐观锁 026 hibernate 操作树形结构 027 hibernate 查询语言(HQL) 028 hibernate 缓存(性能优化策略) 029 hibernate 抓取策略