hibernate主外键配置

凌风雪雕 2010-09-18 08:49:12
<?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="com.lxitedu.bean">

<class name="Book" table="BOOK">
<id name="id" column="ID">
<generator class="native"/>
</id>

<property name="name"/>
<property name="author"/>
<property name="price"/>
<property name="state"/>
<property name="publishing"/>
<property name="intro"/>
<property name="count"/>
<property name="stocks"/>
<property name="date"/>
<property name="isbn"/>

<many-to-one name="typeId" class="Type" outer-join="true" fetch="select" lazy="false">
<column name="typeId" />
</many-to-one>
<many-to-one name="bookcaseId" class="Bookcase" outer-join="true" fetch="select" lazy="false">
<column name="bookcaseId" />
</many-to-one>

<!--<many-to-one name="typeId" class="Type" column="typeId"
cascade="all" not-null="true"/>
<many-to-one name="bookcaseId" class="Bookcase" column="bookcaseId"
cascade="all" not-null="true"/> -->

</class>

</hibernate-mapping>



当我调用
public Book get(long id) {
Transaction transaction = session.beginTransaction();
Book book = (Book) session.load(Book.class, id);
transaction.commit();
return book;
}
的时候抛异常

public static void main(String[] args) {
Book b = new BookDaoImpl().get(1);
System.out.println(b);
}



异常为:Hibernate: select book0_.ID as ID4_0_, book0_.name as name4_0_, book0_.author as author4_0_, book0_.price as price4_0_, book0_.state as state4_0_, book0_.publishing as publishing4_0_, book0_.intro as intro4_0_, book0_.count as count4_0_, book0_.stocks as stocks4_0_, book0_.date as date4_0_, book0_.isbn as isbn4_0_, book0_.typeId as typeId4_0_, book0_.bookcaseId as bookcaseId4_0_ from BOOK book0_ where book0_.ID=?
Hibernate: select type0_.ID as ID6_0_, type0_.number as number6_0_, type0_.name as name6_0_, type0_.description as descript4_6_0_ from TYPE type0_ where type0_.ID=?
Hibernate: select bookcase0_.ID as ID5_0_, bookcase0_.number as number5_0_, bookcase0_.name as name5_0_, bookcase0_.description as descript4_5_0_ from BOOKCASE bookcase0_ where bookcase0_.ID=?
Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.lxitedu.bean.Book.typeId
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:128)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:586)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:231)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3824)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:153)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
at org.hibernate.loader.Loader.doQuery(Loader.java:773)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1933)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:76)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3270)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:496)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:477)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:147)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:1018)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:176)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:191)
at com.lxitedu.bean.Book_$$_javassist_5.toString(Book_$$_javassist_5.java)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at com.lxitedu.dao.hibernate.impl.BookDaoImpl.main(BookDaoImpl.java:76)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:66)
... 24 more


请问到底该如何配置,兄弟我搞了一天了。
...全文
245 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
你的配置错了 多看看hibernate的配置吧
回复
风影萧诺 2010-10-01
我有点不会用xml模式
回复
jeky_zhang2013 2010-09-30
报错里面有类型不匹配的问题,可以检查下
回复
凌风雪雕 2010-09-30
哦 用get就可以了 知道了
回复
xyytuo 2010-09-20
回复
zhy0511 2010-09-19
Book book = (Book) session.load(Book.class, id);
貌似你用哪个的是load...
回复
不善^ 2010-09-19
参数传递错误,检查参数是否正确
回复
凌风雪雕 2010-09-19
set get方法都写了的啊 而且我是使用get的啊 我一般很小使用load的
回复
zhaoqiang4153 2010-09-18
Book.typeId get/set方法写了没有,看看实体类
回复
kaida_7 2010-09-18
按照异常提示,程序当中有懒加载异常,首先你可以直接这个改:
把:session.load(Book.class, id);改为session.get(Book.class, id);用get的试试。。。
第二:就是把你Book中所有信息都提取出来之后,在transaction。commit();这样试试。。。
(这个在WEB中都会用到opensessioninview的)
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-09-18 08:49
社区公告
暂无公告