该帖已经被评为精华帖
|
|
---|---|
作者 | 正文 |
发表时间:2003-12-25
hibernate入门篇之新增功能_3:one-to-many ,我们来介绍一下:many-to-many
接上一篇Work.java package com.javamodel.hibernate; import java.util.HashSet; import java.util.Set; public class Work { private String id = null; private String title = null; private Set authors = new HashSet();; public Work();{} /** * @return */ public Set getAuthors(); { return authors; } /** * @return */ public String getId(); { return id; } /** * @return */ public String getTitle(); { return title; } /** * @param set */ public void setAuthors(Set set); { authors = set; } /** * @param string */ public void setId(String string); { id = string; } /** * @param string */ public void setTitle(String string); { title = string; } } work.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <class name="com.javamodel.hibernate.Work" table="work" > <id name="id" column="id"> <generator class="uuid.hex"/> </id> <property name="title" column="title" /> <set name="authors" table="author_work" inverse="true"> <key column="work_id"/> <many-to-many column="author_id" class="com.javamodel.hibernate.Author" /> </set> </class> </hibernate-mapping> Author.java加上 private Set works = new HashSet();;//get,set author.hbm.xml加上 <set name="works" table="author_work"> <key column="author_id"/> <many-to-many column="work_id" class="com.javamodel.hibernate.Work" /> </set> Example.java package com.javamodel.hibernate; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.log4j.Logger; 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 Logger logger = Logger.getLogger(Example.class);; 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.addClass(Work.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 person1 = new Person();; person1.setName("HengfeiDo1");; person1.setEmail("smallduzi@sohu.com");; Person person2 = new Person();; person2.setName("HengfeiDo2");; person2.setEmail("smallduzi@sohu.com");; Person person3 = new Person();; person3.setName("HengfeiDo3");; person3.setEmail("smallduzi@sohu.com");; Person person4 = new Person();; person4.setName("HengfeiDo4");; person4.setEmail("smallduzi@sohu.com");; Publication publication1 = new Publication();; publication1.setBookName("AAA");; publication1.setDataTime("20031224");; Publication publication2 = new Publication();; publication2.setBookName("BBB");; publication2.setDataTime("20031225");; Publication publication3 = new Publication();; publication3.setBookName("CCC");; publication3.setDataTime("20031226");; Publication publication4 = new Publication();; publication4.setBookName("DDD");; publication4.setDataTime("20031227");; Publication publication5 = new Publication();; publication5.setBookName("EEE");; publication5.setDataTime("20031228");; Publication publication6 = new Publication();; publication6.setBookName("FFF");; publication6.setDataTime("20031229");; Publication publication7 = new Publication();; publication7.setBookName("GGG");; publication7.setDataTime("20031230");; Publication publication8 = new Publication();; publication8.setBookName("HHH");; publication8.setDataTime("20031231");; Author author1 = new Author();; author1.setAlias("smallduzi");; author1.setPerson(person1);; author1.getPublications();.add(publication1);; author1.getPublications();.add(publication2);; Author author2 = new Author();; author2.setAlias("adu");; author2.setPerson(person2);; author2.getPublications();.add(publication3);; author2.getPublications();.add(publication4);; Author author3 = new Author();; author3.setAlias("dududu");; author3.setPerson(person3);; author3.getPublications();.add(publication5);; author3.getPublications();.add(publication6);; Author author4 = new Author();; author4.setAlias("xiaoduzi");; author4.setPerson(person4);; author4.getPublications();.add(publication7);; author4.getPublications();.add(publication8);; publication1.setAuthor(author1);; publication2.setAuthor(author1);; publication3.setAuthor(author2);; publication4.setAuthor(author2);; publication5.setAuthor(author3);; publication6.setAuthor(author3);; publication7.setAuthor(author4);; publication8.setAuthor(author4);; Work work1 = new Work();; work1.setTitle("111111");; work1.getAuthors();.add(author1);; work1.getAuthors();.add(author2);; Work work2 = new Work();; work2.setTitle("222222");; work2.getAuthors();.add(author3);; work2.getAuthors();.add(author4);; Work work3 = new Work();; work3.setTitle("333333");; work3.getAuthors();.add(author1);; work3.getAuthors();.add(author2);; Work work4 = new Work();; work4.setTitle("444444");; work4.getAuthors();.add(author3);; work4.getAuthors();.add(author4);; author1.getWorks();.add(work1);; author1.getWorks();.add(work3);; author2.getWorks();.add(work2);; author2.getWorks();.add(work4);; Session session = _sessions.openSession();; Transaction tx = null; try{ tx = session.beginTransaction();; session.save(author1);; session.save(author2);; session.save(author3);; session.save(author4);; session.save(work1);; session.save(work2);; session.save(work3);; session.save(work4);; tx.commit();; System.out.println("---over---");; }catch(HibernateException he);{ if(tx != null); tx.rollback();; throw he; } finally{ session.close();; } } } 例子写的比较罗嗦。 参考: Hibernate2 Reference Documentation Version 2.1.1 P116 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2003-12-26
我不知道你有没有测试过,这段程序可能有问题:
1 双向的many-to-many肯定要有一端设置为inverse="true",另一端为inverse="false"(默认) 2 你没有设置cascade="save-update",这在insert时十有八九要违反外键约束,不要告诉我你用的是mysql myisam。 3 既然是双向的many-to-many,那一定要保持两端同时更新,即 work.getAuthors().add(author); author.getWorks().add(work); 一定要成对出现。 |
|
返回顶楼 | |
发表时间:2003-12-26
1.不清楚,我侧过的没有问题。
2.没有设置,数据库用的oracle8.1.7。但是能保存进去。 3. 不成对也不能保存。 多谢你提出的宝贵意见,做事要严谨些。我没有设置外键(偷懒)。 |
|
返回顶楼 | |
发表时间:2003-12-26
如果还有什么不当之处,请忙上指正。我可不想误人子弟。
如果有什么不当之处,我先在这里说一声:“对不起了”。 |
|
返回顶楼 | |
发表时间:2003-12-26
你的程序在我这是通不过的,连接表主键从突,这是因为你没有设置一端为inverse="true"的缘故。
|
|
返回顶楼 | |
发表时间:2003-12-26
谢谢yehs220的认真测试。忙上改
|
|
返回顶楼 | |
发表时间:2003-12-26
不过很奇怪,我这里还是可以通过的。
|
|
返回顶楼 | |
发表时间:2003-12-29
这个例子我有一些疑问,除了第3的publication的问题之外,在执行的时候,还有
net.sf.hibernate.JDBCException: could not insert collection: [com.javamodel.hibernate.Author.works#40288095f9c21a2200f9c21a29b20001]Caused by: java.sql.SQLException: General error, message from server: "Table 'hibernate.works' doesn't exist" 由于我是用 SchemaExport 建立tables的,所以才有这个问题吧,不知道如何解决这个问题呢? 虽然在执行的时候出现问题,但是我的资料库里头有 author,person,publication,work 4个tables,而work里头也有资料呢。真是神奇!!而publication呢,则和第3个例子一样,没有资料在里面的,要session.save(publication1);之后才有。 ...... 另外还有一个问题,如果我要把其中一个author的资料全部列出来,那应该怎样做阿??我用session.load()总是失败。 |
|
返回顶楼 | |
发表时间:2004-01-12
比如说,Author1参加Work1的时间是2003.4.24日,我想记录这个时间
,应当记录在中间表里面,能否使用many-to-many |
|
返回顶楼 | |
发表时间:2004-01-15
author.hbm.xml 如下:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <class name="com.javamodel.hibernate.Author" table="author" > <id name="id" column="id"> <generator class="foreign"> <param name="property">person</param> </generator> </id> <property name="alias" column="alias" /> <one-to-one name="person" class="com.javamodel.hibernate.Person" cascade="all" constrained="true" /> <set name="publications" lazy="true" inverse="true" cascade="all" > <key column="authorid"/> <one-to-many class="com.javamodel.hibernate.Publication" /> </set> <set name="works" table="author_work" inverse="true"> <key column="author_id"/> <many-to-many column="work_id" class="com.javamodel.hibernate.Work" /> </set> </class> </hibernate-mapping> work.hbm.xml如下: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <class name="com.javamodel.hibernate.Work" table="work" > <id name="id" column="id"> <generator class="uuid.hex"/> </id> <property name="title" column="title" /> <set name="authors" table="author_work" inverse="true"> <key column="work_id"/> <many-to-many column="author_id" class="com.javamodel.hibernate.Author" /> </set> </class> </hibernate-mapping> |
|
返回顶楼 | |