读书人

Hibernate投射文件

发布时间: 2011-12-05 22:12:57 作者: rapoo

Hibernate映射文件
我映射文件和持久化类是自动生成的
<?xml version= "1.0 " encoding= "utf-8 "?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN "
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name= "com.Request " table= "request " schema= "callback " catalog= "callback ">
<composite-id name= "id " class= "com.RequestId ">
<key-property name= "id " type= "long ">
<column name= "id " />
</key-property>
<key-property name= "acctId " type= "string ">
<column name= "AcctId " length= "40 " />
</key-property>
<key-property name= "primaryphone " type= "string ">
<column name= "primaryphone " length= "50 " />
</key-property>
<key-property name= "bundlephone1 " type= "string ">
<column name= "bundlephone1 " length= "50 " />
</key-property>
<key-property name= "bundlephone2 " type= "string ">
<column name= "bundlephone2 " length= "50 " />
</key-property>
<key-property name= "numcard " type= "integer ">
<column name= "numcard " />


</key-property>
</composite-id>
</class>
</hibernate-mapping>


Request类
public class Request implements java.io.Serializable {


// Fields

private RequestId id;


// Constructors

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


/** full constructor */
public Request(RequestId id) {
this.id = id;
}




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

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

public class RequestId implements java.io.Serializable {


// Fields

private long id;
private String acctId;
private String primaryphone;
private String bundlephone1;
private String bundlephone2;
private Integer numcard;


// Constructors

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


/** full constructor */
public RequestId(long id, String primaryphone, String bundlephone1, String bundlephone2, Integer numcard) {
this.id = id;
this.requesttype = requesttype;
this.primaryphone = primaryphone;
this.bundlephone1 = bundlephone1;
this.bundlephone2 = bundlephone2;
this.numcard = numcard;


}


// Property accessors

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

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


public String getAcctId() {
return this.acctId;
}

public void setAcctId(String acctId) {
this.acctId = acctId;
}


public String getPrimaryphone() {
return this.primaryphone;
}

public void setPrimaryphone(String primaryphone) {
this.primaryphone = primaryphone;
}

public String getBundlephone1() {
return this.bundlephone1;
}

public void setBundlephone1(String bundlephone1) {
this.bundlephone1 = bundlephone1;
}

public String getBundlephone2() {
return this.bundlephone2;
}

public void setBundlephone2(String bundlephone2) {
this.bundlephone2 = bundlephone2;
}

public Integer getNumcard() {
return this.numcard;
}

public void setNumcard(Integer numcard) {
this.numcard = numcard;
}
}

测试类为
public static void main(String[] args) {
Session session =HibernateSessionFactory.getSession();

String hql= "from Request request ";
System.out.println(hql);
Query query=session.createQuery(hql);

List list=query.list();

Iterator it=list.iterator();
int k=0;
while(it.hasNext()){
Object obj=(Object)it.next();


Request request=(Request)obj;

k=k+1;
System.out.println( 'K= "+k);
if(request !=null){
RequestId requestid=request.getId();
System.out.println(requestid.getBundlephone1()+ "\t "+requestid.getBundlephone2()+ "\t "+
"课程: "+requestid.getBundlephone2());
}else{
System.out.println( "没数据null ");
}
}
}

运行结果为:
K=2
没数据null


(说明:数据库中是有数据的,但不知道为什么会打印 "没数据null ")




[解决办法]
帮顶
[解决办法]
K=2证明有数据啊,不知道楼主是什么问题
在List list=query.list();后面加上 session.flush();
[解决办法]
Session session =HibernateSessionFactory.getSession();
String hql="from Request AS request"; (加上AS)
System.out.println(hql);
Transaction transaction=session.beginTransaction();
Query query=session.createQuery(hql);
list=query.list();
session.flush();
tx.commit();
[解决办法]

Java code
Session   session   =HibernateSessionFactory.getSession();  String   hql="from   Request AS request"; [color=#FF0000](加上AS)[/color] System.out.println(hql);     Transaction transaction=session.beginTransaction();     Query   query=session.createQuery(hql);       list=query.list();      session.flush(); tx.commit();
[解决办法]
运行结果怎么没有 K=1 的时候呢?
[解决办法]
String hql="from Request as request";


[解决办法]
查询不需要事务的吧?
[解决办法]
bangding le

读书人网 >Java Web开发

热点推荐