先看由满江红翻译团队(RedSaga Translate Team)翻译的一对多配置说明
然后在看例子
一对多关联(One-to-many Associations)
一对多关联 通过外键 连接两个类对应的表,而没有中间集合表。 这个关系模型失去了一些Java集合的语义:
一个从Product到Part的关联需要关键字字段,可能还有一个索引字段指向Part所对应的表。 <one-to-many></one-to-many>标记指明了一个一对多的关联。
<one-to-many class="ClassName"></one-to-many>(1)
not-found="ignore|exception" (2)
entity-name="EntityName" (3)
node="element-name"
embed-xml="true|false"
/>
(1) | class(必须):被关联类的名称。 |
(2) | not-found (可选 - 默认为exception): 指明若缓存的标示值关联的行缺失,该如何处理: ignore 会把缺失的行作为一个空关联处理。 |
(3) | entity-name (可选): 被关联的类的实体名,作为class的替代。 |
例子
<set name="bars">
<key column="foo_id"/>
<one-to-many class="org.hibernate.Bar"/>
</set>
注意:<one-to-many>元素不需要定义任何字段。 也不需要指定表名。
重要提示 :如果一对多关联中的外键字段定义成NOT NULL,你必须把<key>映射声明为not-null="true",或者使用双向关联,并且标明inverse="true"。
详细请看http://www.huihoo.com/framework/hibernate/reference-v3_zh-cn/collections.html
1 先建表
create table student
(sid varchar ( 32 ) not null primary key ,
sname varchar ( 16 ),
sage varchar ( 16 ),
)
create table book
(bid varchar ( 32 ) not null primary key ,
bname varchar ( 16 ),
bprice varchar ( 16 ),
sid varchar ( 32 )
)
2.写vo
Student.java
package
com.test;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
java.util.Set;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
public
class
Student
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
{
private String sid;
private String sname;
private String sage;
private Set book;
public Student()
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
}
// 写上get set
Book.JAVA
package
com.test;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
public
class
Book
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
{
private String bid;
private String bname;
private String bprice;
public Book()
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
}
//写上get set
3.写对应的映射文件
Student.hbm.xml
<?
xml version="1.0"
?>
<!
DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<
hibernate-mapping
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<
class
name
="com.test.Student"
table
="student"
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<
id
name
="sid"
type
="string"
unsaved-value
="null"
>
<
column
name
="sid"
sql-type
="char(32)"
not-null
="true"
/>
<
generator
class
="uuid.hex"
/>
</
id
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<
property
name
="sname"
>
<
column
name
="sname"
sql-type
="varchar(16)"
not-null
="true"
/>
</
property
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<
property
name
="sage"
>
<
column
name
="sage"
sql-type
="varchar(16)"
not-null
="true"
/>
</
property
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<
set
name
="book"
cascade
="all"
outer-join
="true"
>
<
key
column
="sid"
/>
<
one-to-many
class
="com.test.Book"
/>
</
set
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
</
class
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
</
hibernate-mapping
>
Book.hbm.xml
<?
xml version="1.0"
?>
<!
DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<
hibernate-mapping
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<
class
name
="com.test.Book"
table
="book"
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<
id
name
="bid"
type
="string"
unsaved-value
="null"
>
<
column
name
="bid"
sql-type
="char(32)"
not-null
="true"
/>
<
generator
class
="uuid.hex"
/>
</
id
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<
property
name
="bname"
>
<
column
name
="bname"
sql-type
="varchar(16)"
not-null
="true"
/>
</
property
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
<
property
name
="bprice"
>
<
column
name
="bprice"
sql-type
="varchar(16)"
not-null
="true"
/>
</
property
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
</
class
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
</
hibernate-mapping
>
接着把下面的hibernate.properties文件拷到classes目录下。。这里用的是mysql
hibernate.query.substitutions true 1, false 0, yes 'Y', no 'N'
## MySQL
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class org.gjt.mm.mysql.Driver
hibernate.connection.url jdbc:mysql://localhost:3306/wjcms
hibernate.connection.username root
hibernate.connection.password wujun
hibernate.connection.pool_size 1
hibernate.proxool.pool_alias pool1
hibernate.show_sql true
hibernate.jdbc.batch_size 0
hibernate.max_fetch_depth 1
hibernate.cache.use_query_cache true
4.写测试类了..
package
com.test;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
net.sf.hibernate.Session;
import
net.sf.hibernate.SessionFactory;
import
net.sf.hibernate.cfg.Configuration;
import
net.sf.hibernate.
*
;
import
java.util.Set;
import
java.util.HashSet;
import
java.sql.
*
;
import
java.util.List;
import
java.util.Iterator;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
public
class
TestOneToMany
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
{
SessionFactory sf;
Session session;
public TestOneToMany()
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
try
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
Configuration cfg = new Configuration();
sf = cfg.addClass(Student.class).addClass(Book.class).buildSessionFactory();
}
catch(HibernateException ex)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
ex.printStackTrace();
}
}
//插入
public void doCreate()
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
try
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
session = sf.openSession();
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
Student student = new Student();
student.setSname("小王");
student.setSage("22");
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
Set bookSet = new HashSet();
Book book = null;
for(int i=0;i<2;i++)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
book = new Book();
book.setBname("java "+i);
book.setBprice("50");
bookSet.add(book);
}
student.setBook(bookSet);
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
session.save(student);
session.flush();
session.connection().commit();
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
}
catch(HibernateException ex)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
ex.printStackTrace();
}
catch(SQLException ex1)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
ex1.printStackTrace();
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
try
{
session.close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)