hibernate的一个问题,请大家帮助

请大家看下为什么报错,怎样解决?
[color=green]Book.java[/color]
package bean;

public class Book implements java.io.Serializable {


private Integer bookId;
private String bookName;
private String bookAuthor;
private double bookPrice;


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


/** full constructor */
public Book(String bookName, String bookAuthor, double bookPrice) {
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookPrice = bookPrice;
}


// Property accessors

public Integer getBookId() {
return this.bookId;
}

public void setBookId(Integer bookId) {
this.bookId = bookId;
}

public String getBookName() {
return this.bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public String getBookAuthor() {
return this.bookAuthor;
}

public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}

public double getBookPrice() {
return this.bookPrice;
}

public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}

}
[color=green]Book.hbm.xml[/color]
<?xml version="1.0"?>
<!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 - Hibernate Tools
-->
<hibernate-mapping>
<class name="bean.Book" table="book" catalog="bookstoressh">
<id name="bookId" type="integer">
<column name="book_id" />
<generator class="native" />
</id>
<property name="bookName" type="string">
<column name="book_name" length="100" not-null="true" />
</property>
<property name="bookAuthor" type="string">
<column name="book_author" length="100" not-null="true" />
</property>
<property name="bookPrice" type="double">
<column name="book_price" precision="22" scale="0" not-null="true" />
</property>
</class>
</hibernate-mapping>

[color=green]hibernate.cfg.xml[/color]
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="myeclipse.connection.profile">
bookstoressh
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/bookstoressh
</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">
org.gjt.mm.mysql.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">true</property>
<mapping resource="bean/Book.hbm.xml" />

</session-factory>

</hibernate-configuration>
报错情况:
Hibernate: select book0_.book_id as book1_0_, book0_.book_name as book2_0_0_, book0_.book_author as book3_0_0_, book0_.book_price as book4_0_0_ from bookstoressh.book book0_ where book0_.book_id=?
org.hibernate.HibernateException: More than one row with the given identifier was found: 2, for class: bean.Book
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:137)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:101)
at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersister.java:2471)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:351)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:332)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:113)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:167)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:79)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:621)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:614)
at service.Work.sec(Work.java:22)
at service.Work.main(Work.java:40)
[color=green]业务类 Work.java[/color]
package service;


import org.hibernate.Session;
import org.hibernate.Transaction;

import bean.Book;
import bean.HibernateSessionFactory;


public class Work {
void sec(){

Session session = null ;
try {

session = HibernateSessionFactory.getCurrentSession();
Transaction f = session.beginTransaction();
[color=red] [size=18] Book book=(Book)session.get( Book.class,new Integer(2));//引起异常的地方,????[/size] System.out.println(book.getBookName());[/color]

f.commit();

} catch (Exception hex) {
hex.printStackTrace();
} finally {
if (session != null) {
HibernateSessionFactory.closeCurrentSession();

}

}}


public static void main(String[] args) {
Work work=new Work();
work.sec();

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值