| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3957 人关注过本帖
标题:hibernate配置问题!
取消只看楼主 加入收藏
亮剑
Rank: 1
等 级:新手上路
帖 子:148
专家分:0
注 册:2006-10-18
收藏
 问题点数:0 回复次数:4 
hibernate配置问题!

------------------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)

搜索更多相关主题的帖子: Integer orderline public hibernate OrderLine 
2007-04-02 20:43
亮剑
Rank: 1
等 级:新手上路
帖 子:148
专家分:0
注 册:2006-10-18
收藏
得分:0 

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.html }.
*/
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编辑过]

2007-04-02 21:06
亮剑
Rank: 1
等 级:新手上路
帖 子:148
专家分:0
注 册:2006-10-18
收藏
得分:0 
我把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=?
2007-04-02 21:51
亮剑
Rank: 1
等 级:新手上路
帖 子:148
专家分:0
注 册:2006-10-18
收藏
得分:0 

我的整个映射关系是:
Product 与orderline是1对1的双向关系
Prdouct里有个属性orderline
orderline里有个属性product
orderline与order有1对多的双向关系
orderline里有个属性order
order里有个orderline集合
order与user有1对多的双向关系
order里还有个user属性
user里有个order的集合

我这么映射有问题吗???

2007-04-02 21:58
亮剑
Rank: 1
等 级:新手上路
帖 子:148
专家分:0
注 册:2006-10-18
收藏
得分:0 
我把product里的<one-to-one>给去了问题就没了 为什么啊
不明白啊晕啊!!!!!!!!!!
2007-04-02 22:00
快速回复:hibernate配置问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023647 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved