------------------Product实体------------------------------
public class Product implements Serializable{
private Integer id;
private OrderLine orderline;
public OrderLine getOrderline() {
return orderline;
}
public void setOrderline(OrderLine orderline) {
this.orderline = orderline;
}
public Integer getId() {
return id;
}
public void setId(Integer id){
this.id = id;
}
}
----------------OrderLine实体------------------------------
public class OrderLine implements Serializable{
private Integer id;
private Product product;
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Integer getId() {
return id;
}
public void setId(Integer id){
this.id = id;
}
}
-----------------------Product___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 package="cn.com.ecport.domain">
<class name="Product" table="product">
<id name="id" column="id" type="java.lang.Integer">
<generator class="increment" />
</id>
<one-to-one name="orderline"
class="OrderLine"
property-ref="product"
cascade="all"></one-to-one>
</class>
</hibernate-mapping>
-----------------------OrderLine___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 package="cn.com.ecport.domain">
<class name="OrderLine" table="orderline">
<id name="id" column="id" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<many-to-one name="product"
class="Product"
column="product_id"
></many-to-one>
</class>
</hibernate-mapping>
---------------------为什么会有这个异常----------------------------------------------
当我执行List listuser =new ProductDAOImpl().findAll();时报异常---》如下(我的配置哪里写的有问题请指点)
Exception in thread "main" org.hibernate.HibernateException: More than one row with the given identifier was found: 3, for class: cn.com.ecport.domain.OrderLine
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:69)
at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:85)
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1596)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:608)
at org.hibernate.type.EntityType.resolve(EntityType.java:382)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2211)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
at org.hibernate.loader.Loader.list(Loader.java:2090)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:388)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at cn.com.ecport.dao.daoimpl.ProductDAOImpl.findAll(ProductDAOImpl.java:35)
at cn.com.ecport.dao.daoimpl.ProductDAOImpl.main(ProductDAOImpl.java:47)
----------------解决方案--------------------------------------------------------
More than one row with the given identifier was found: 3, for class: cn.com.ecport.domain.OrderLine
----------------解决方案--------------------------------------------------------
楼主代码没发全吧,最有用的那段SessionFactory的那段怎么没发上来,这个问题应该是你一个session里保存了两个对象。看看把transaction那段修改修改,有的时候一个session就是有问题,我也不知道为什么,而有的时候就可以,我不知道是不是transaction的时候我没写好,那时时间紧,没细研究,但是就是session加载对象的问题。
----------------解决方案--------------------------------------------------------
SessionFactory类
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42 }.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
[此贴子已经被作者于2007-4-2 21:08:08编辑过]
----------------解决方案--------------------------------------------------------
我知道,我的意思是说你用同一个session加载了两个对象
----------------解决方案--------------------------------------------------------
问题应该在这行
楼主用了同一个threadlocal
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
----------------解决方案--------------------------------------------------------
得回家了。。。
----------------解决方案--------------------------------------------------------
我把SQL语句打了出来发现他执行了三次select orderlines 这是为什么 怎么会有这样的事情啊
Hibernate:
select
product0_.id as id0_,
product0_.name as name0_,
product0_.description as descript3_0_,
product0_.baseprice as baseprice0_,
product0_.category_id as category5_0_,
product0_.writer as writer0_,
product0_.publish as publish0_,
product0_.pages as pages0_,
product0_.images as images0_
from
product product0_
Hibernate:
select
orderline0_.id as id5_0_,
orderline0_.amount as amount5_0_,
orderline0_.order_id as order3_5_0_,
orderline0_.product_id as product4_5_0_
from
orderline orderline0_
where
orderline0_.product_id=?
Hibernate:
select
orderline0_.id as id5_0_,
orderline0_.amount as amount5_0_,
orderline0_.order_id as order3_5_0_,
orderline0_.product_id as product4_5_0_
from
orderline orderline0_
where
orderline0_.product_id=?
Hibernate:
select
orderline0_.id as id5_0_,
orderline0_.amount as amount5_0_,
orderline0_.order_id as order3_5_0_,
orderline0_.product_id as product4_5_0_
from
orderline orderline0_
where
orderline0_.product_id=?
----------------解决方案--------------------------------------------------------
我的整个映射关系是:
Product 与orderline是1对1的双向关系
Prdouct里有个属性orderline
orderline里有个属性product
orderline与order有1对多的双向关系
orderline里有个属性order
order里有个orderline集合
order与user有1对多的双向关系
order里还有个user属性
user里有个order的集合
我这么映射有问题吗???
----------------解决方案--------------------------------------------------------
我把product里的<one-to-one>给去了问题就没了 为什么啊
不明白啊晕啊!!!!!!!!!!
----------------解决方案--------------------------------------------------------