|
该帖已经被评为精华帖
|
|
|---|---|
| 作者 | 正文 |
|
时间:2003-12-24
接上一篇文章hibernate入门篇之新增功能_2:one-to-one。我们再继续进行!(为什么要说再呢?)
Publication.java [code:1] package com.javamodel.hibernate; public class Publication { private String id = null; private String bookName = null; private String dataTime = null; private String authorId = null; private Author author = null; public Publication(){} /** * @return */ public String getBookName() { return bookName; } /** * @return */ public String getDataTime() { return dataTime; } /** * @return */ public String getId() { return id; } /** * @param string */ public void setBookName(String string) { bookName = string; } /** * @param string */ public void setDataTime(String string) { dataTime = string; } /** * @param string */ public void setId(String string) { id = string; } /** * @return */ public String getAuthorId() { return authorId; } /** * @param string */ public void setAuthorId(String string) { authorId = string; } /** * @return */ public Author getAuthor() { return author; } /** * @param author */ public void setAuthor(Author author) { this.author = author; } }[/code:1] Publication.hbm.xml [code:1] <?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <class name="com.javamodel.hibernate.Publication" table="publication"> <id name="id" column="id"> <generator class="uuid.hex"/> </id> <property name="bookName" column="bookname" /> <property name="dataTime" column="datatime" /> <many-to-one name="author" column="authorid" /> </class> </hibernate-mapping> [/code:1] Author.java加上 [code:1] private Set publications = new HashSet();//add get,set [/code:1] author.hbm.xml加上 [code:1] <set name="publications" lazy="true" inverse="true" cascade="all" > <key column="authorid"/> <one-to-many class="com.javamodel.hibernate.Publication" /> </set> [/code:1] Example.java [code:1] package com.javamodel.hibernate; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import net.sf.hibernate.HibernateException; import net.sf.hibernate.MappingException; import net.sf.hibernate.Session; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.Transaction; import net.sf.hibernate.cfg.Configuration; public class Example{ private static SessionFactory _sessions = null; private static Properties pops = new Properties(); static{ try { InputStream stream = Example.class.getResourceAsStream("hibernate.properties"); try { pops.load(stream); } catch (IOException e1) { e1.printStackTrace(); } Configuration cfg = new Configuration(); cfg.addClass(Person.class); cfg.addClass(Author.class); cfg.addClass(Publication.class); cfg.setProperties(pops); _sessions = cfg.buildSessionFactory(); } catch (MappingException e) { e.printStackTrace(); } catch (HibernateException e) { e.printStackTrace(); } } public static void main(String[] args) throws HibernateException { Person person = new Person(); person.setName("HengfeiDo"); person.setEmail("smallduzi@sohu.com"); Publication publication1 = new Publication(); publication1.setBookName("AAA"); publication1.setDataTime("20031224"); Publication publication2 = new Publication(); publication2.setBookName("BBB"); publication2.setDataTime("20031225"); Author author = new Author(); author.setAlias("smallduzi"); author.setPerson(person); author.getPublications().add(publication1); author.getPublications().add(publication2); publication1.setAuthor(author); publication2.setAuthor(author); Session session = _sessions.openSession(); Transaction tx = null; try{ tx = session.beginTransaction(); session.save(author); tx.commit(); System.out.println("over"); }catch(HibernateException he){ if(tx != null) tx.rollback(); throw he; } finally{ session.close(); } } } [/code:1] 下一篇,我们介绍many-to-many 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2003-12-24
如果转载,请注明:中国Hibernate技术专业论坛。
一经发现打50大板。 |
|
| 返回顶楼 | |
|
时间:2003-12-25
其他不需要save();
这几个例子,经过本人的严格测试。放心不会误人子弟的。 耐心的是一下吧。 |
|
| 返回顶楼 | |
|
时间:2003-12-29
这个例子我有一些问题呢,执行后,我检查publication这个table,里头没有资料呢。 :(
(我用 select * from publication;他说empty set) 不过我加入 session.save(publication1); session.save(publication2); 之后,才可以在资料库找此2笔资料呢。 我本身是用MySQL的,不知道是否这个问题呢?? 我把 private Set publications = new HashSet(); 加在前面。 private String id ; private String alias = null; private Person person = null; private Set publications = new HashSet(); 然后在后面加入 public void setPublications(Set publications) { this.publications=publications; } public Set getPublications() { return publications; } 不知道是哪里出错了呢。 因为 smallduzi 兄说其它的不必save()呢。 |
|
| 返回顶楼 | |
|
时间:2004-01-07
<set name="publications" lazy="true" inverse="true" cascade="all" >
<key column="authorid"/> <one-to-many class="com.javamodel.hibernate.Publication" /> </set> 请各位解释一下上面!:)还有下面也一起! InputStream stream = Example.class.getResourceAsStream("hibernate.properties"); pops.load(stream); 谢了 |
|
| 返回顶楼 | |
|
时间:2004-01-08
我的也是这个问题,只有一个表有数据,其他的表都没有数据,请作者解释一下
|
|
| 返回顶楼 | |
|
时间:2004-01-09
解释什么呢?:)
|
|
| 返回顶楼 | |
|
时间:2004-01-10
ft,我更郁闷,我one的那个婊insert成功了,many那个表应该有2条的,不知道为什么只有1条
|
|
| 返回顶楼 | |
|
时间:2004-01-12
回应 smallduzi 兄,就是您说说的,“其它的不必save()”,我也发问了。
如果其他的我没有save()的话,那么纪录里头是empty set, (我用select * from XX检查),必须把所有东西都save()才可以把资料save到database里头。 不知道是不是database不同的关系。 |
|
| 返回顶楼 | |
|
时间:2004-01-13
我自己做的一个one to many例子中,查询没有问题,也是保存有问题。
如果只保存一个对象,关联的对象是没有保存的。 请楼主解惑帮忙,分析可能会出现这种问题的原因。 谢谢!期待中呀! |
|
| 返回顶楼 | |






