当前位置: 代码迷 >> SQL >> 困惑了几天的有关问题 为什么hibernate执行了相同的SQL语句
  详细解决方案

困惑了几天的有关问题 为什么hibernate执行了相同的SQL语句

热度:30   发布时间:2016-05-05 13:09:43.0
困惑了几天的问题 为什么hibernate执行了相同的SQL语句
 package cn.phping.one2one_fk_2;public class Passport_fk_2 {	private int id;	private String serial;	private int expiry;	private Person_fk_2 person_fk_2 ;  	public int getId() {		return id;	}	public void setId(int id) {		this.id = id;	}	public String getSerial() {		return serial;	}	public void setSerial(String serial) {		this.serial = serial;	}	public int getExpiry() {		return expiry;	}	public void setExpiry(int expiry) {		this.expiry = expiry;	}	public Person_fk_2 getPerson_fk_2() {		return person_fk_2;	}	public void setPerson_fk_2(Person_fk_2 person_fk_2) {		this.person_fk_2 = person_fk_2;	}}package cn.phping.one2one_fk_2;public class Person_fk_2 {	private int id;	private String name;	private Passport_fk_2 passport_fk_2;	public int getId() {		return id;	}	public void setId(int id) {		this.id = id;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public Passport_fk_2 getPassport_fk_2() {		return passport_fk_2;	}	public void setPassport_fk_2(Passport_fk_2 passport_fk_2) {		this.passport_fk_2 = passport_fk_2;	}	}


<?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"><hibernate-mapping ><class name="cn.phping.one2one_fk_2.Passport_fk_2"><id name="id">  <generator class="native">  </generator></id><property name="serial" /><property name="expiry" /><one-to-one name="person_fk_2" class="cn.phping.one2one_fk_2.Person_fk_2"  property-ref="passport_fk_2"></one-to-one></class></hibernate-mapping><?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"><hibernate-mapping><class name="cn.phping.one2one_fk_2.Person_fk_2">      <id name="id">      <generator class="native"></generator> </id><property name="name" type="java.lang.String"/><many-to-one name="passport_fk_2"  unique="true"></many-to-one></class></hibernate-mapping>

以上是两个POJO类,以及两个映射文件,两者关系是基于唯一外键的一对一关系,然后我写了测试类 用junit测试
package cn.phping.one2one_fk_2;import java.util.Iterator;import java.util.List;import junit.framework.TestCase;import org.hibernate.Session;import cn.phping.junit.HibernateUtils;public class Testone2one_fk_2 extends TestCase {	Session session = null;	@Override	protected void setUp() throws Exception {		session = HibernateUtils.currentSession();	}	@Override	protected void tearDown() throws Exception {	      HibernateUtils.closeSession();	}	public void  testSaveone2one_fk_2() {		session.beginTransaction();		Person_fk_2 person_fk_2 = new Person_fk_2();		person_fk_2.setName("wuxuping");		Passport_fk_2 passport_fk_2  = new Passport_fk_2();		passport_fk_2.setSerial("2203222222222");		passport_fk_2.setExpiry(25);		passport_fk_2.setPerson_fk_2(person_fk_2);		session.save(passport_fk_2);		session.getTransaction().commit();		}		public void testGetDatas(){		session.beginTransaction();		List list = session.createQuery("from Passport_fk_2 p ").list();				}	}

当我测试testGetDatas()方法时,测试的结果如下,当我数据库里有5条Passport_fk_2 hibernate执行了5条相同的语句,当我有100条时,也就有100条相同的sql 。不知道为什么,不知道小弟那里设置有错误
请高手们帮帮我,小弟不胜感激!!!!
Hibernate: select passport_f0_.id as id8_, passport_f0_.serial as serial8_, passport_f0_.expiry as expiry8_ from Passport_fk_2 passport_f0_
Hibernate: select person_fk_0_.id as id9_0_, person_fk_0_.name as name9_0_, person_fk_0_.passport_fk_2 as passport3_9_0_ from Person_fk_2 person_fk_0_ where person_fk_0_.passport_fk_2=?
Hibernate: select person_fk_0_.id as id9_0_, person_fk_0_.name as name9_0_, person_fk_0_.passport_fk_2 as passport3_9_0_ from Person_fk_2 person_fk_0_ where person_fk_0_.passport_fk_2=?
Hibernate: select person_fk_0_.id as id9_0_, person_fk_0_.name as name9_0_, person_fk_0_.passport_fk_2 as passport3_9_0_ from Person_fk_2 person_fk_0_ where person_fk_0_.passport_fk_2=?
Hibernate: select person_fk_0_.id as id9_0_, person_fk_0_.name as name9_0_, person_fk_0_.passport_fk_2 as passport3_9_0_ from Person_fk_2 person_fk_0_ where person_fk_0_.passport_fk_2=?
Hibernate: select person_fk_0_.id as id9_0_, person_fk_0_.name as name9_0_, person_fk_0_.passport_fk_2 as passport3_9_0_ from Person_fk_2 person_fk_0_ where person_fk_0_.passport_fk_2=?
1 楼 movingboy 2008-04-25  
你没看到执行SQL语句末尾的问号吗?那是参数占位符,它会被实际的参数值替代。参数不同取得的结果也就不同了
2 楼 wuxuping 2008-04-25  
可是我并没有添加参数啊 我只是想全部取出来
3 楼 steven_652 2008-04-25  
你开启lazy fetch功能了吗?
4 楼 wuxuping 2008-04-25  
steven_652 写道
你开启lazy fetch功能了吗?

能说的具体点吗 可是别人的代码根本这样没问题啊
5 楼 movingboy 2008-04-25  
两个实体之间的关联配置有错,你仔细检查一下吧
  相关解决方案