Hibernate一对多实例
来源:互联网 发布:英语流利说怎么样知乎 编辑:程序博客网 时间:2024/05/10 22:11
1 先建表
Student.java
Student.hbm.xml
Book.hbm.xml
接着把下面的hibernate.properties文件拷到classes目录下。。这里用的是mysql
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(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 )
)
Student.java
package com.test;
import java.util.Set;
public class Student
{
private String sid;
private String sname;
private String sage;
private Set book;
public Student()
{
}
// 写上get set
Book.JAVAimport java.util.Set;
public class Student
{
private String sid;
private String sname;
private String sage;
private Set book;
public Student()
{
}
// 写上get set
package com.test;
public class Book
{
private String bid;
private String bname;
private String bprice;
public Book()
{
}
//写上get set
3.写对应的映射文件public class Book
{
private String bid;
private String bname;
private String bprice;
public Book()
{
}
//写上get set
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">
<hibernate-mapping>
<class name="com.test.Student" table="student" >
<id name="sid" type="string" unsaved-value="null" >
<column name="sid" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="sname">
<column name="sname" sql-type="varchar(16)" not-null="true"/>
</property>
<property name="sage">
<column name="sage" sql-type="varchar(16)" not-null="true"/>
</property>
<set name="book" cascade="all" outer-join="true">
<key column="sid"/>
<one-to-many class="com.test.Book" />
</set>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.test.Student" table="student" >
<id name="sid" type="string" unsaved-value="null" >
<column name="sid" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="sname">
<column name="sname" sql-type="varchar(16)" not-null="true"/>
</property>
<property name="sage">
<column name="sage" sql-type="varchar(16)" not-null="true"/>
</property>
<set name="book" cascade="all" outer-join="true">
<key column="sid"/>
<one-to-many class="com.test.Book" />
</set>
</class>
</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">
<hibernate-mapping>
<class name="com.test.Book" table="book" >
<id name="bid" type="string" unsaved-value="null" >
<column name="bid" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="bname">
<column name="bname" sql-type="varchar(16)" not-null="true"/>
</property>
<property name="bprice">
<column name="bprice" sql-type="varchar(16)" not-null="true"/>
</property>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.test.Book" table="book" >
<id name="bid" type="string" unsaved-value="null" >
<column name="bid" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="bname">
<column name="bname" sql-type="varchar(16)" not-null="true"/>
</property>
<property name="bprice">
<column name="bprice" sql-type="varchar(16)" not-null="true"/>
</property>
</class>
</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.写测试类了..## 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
package com.test;
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;
public class TestOneToMany
{
SessionFactory sf;
Session session;
public TestOneToMany()
{
try
{
Configuration cfg = new Configuration();
sf = cfg.addClass(Student.class).addClass(Book.class).buildSessionFactory();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
}
//插入
public void doCreate()
{
try
{
session = sf.openSession();
Student student = new Student();
student.setSname("小王");
student.setSage("22");
Set bookSet = new HashSet();
Book book = null;
for(int i=0;i<2;i++)
{
book = new Book();
book.setBname("java "+i);
book.setBprice("50");
bookSet.add(book);
}
student.setBook(bookSet);
session.save(student);
session.flush();
session.connection().commit();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
catch(SQLException ex1)
{
ex1.printStackTrace();
}
finally
{
try{
session.close();
}
catch(HibernateException ex2){
}
}
}
//查询
public void doQuery()
{
try{
session = sf.openSession();
Query q = session.createQuery("select s from Student as s");
List l = q.list();
Student s = null;
Book book = null;
for(int i=0;i<l.size();i++)
{
s = (Student)l.get(i);
System.out.println("姓名: "+s.getSname());
System.out.println("年龄: "+s.getSage());
System.out.println("所有的书:");
Iterator it = s.getBook().iterator();
while(it.hasNext())
{
book = (Book)it.next();
System.out.println("书名: "+book.getBname());
System.out.println("价格: "+book.getBprice());
}
}
}
catch(HibernateException ex){
ex.printStackTrace();
}
finally{
try{
session.close();
}
catch(HibernateException ex2){
}
}
}
public static void main(String[] args)
{
TestOneToMany t = new TestOneToMany();
//t.doCreate();
t.doQuery();
}
}
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;
public class TestOneToMany
{
SessionFactory sf;
Session session;
public TestOneToMany()
{
try
{
Configuration cfg = new Configuration();
sf = cfg.addClass(Student.class).addClass(Book.class).buildSessionFactory();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
}
//插入
public void doCreate()
{
try
{
session = sf.openSession();
Student student = new Student();
student.setSname("小王");
student.setSage("22");
Set bookSet = new HashSet();
Book book = null;
for(int i=0;i<2;i++)
{
book = new Book();
book.setBname("java "+i);
book.setBprice("50");
bookSet.add(book);
}
student.setBook(bookSet);
session.save(student);
session.flush();
session.connection().commit();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
catch(SQLException ex1)
{
ex1.printStackTrace();
}
finally
{
try{
session.close();
}
catch(HibernateException ex2){
}
}
}
//查询
public void doQuery()
{
try{
session = sf.openSession();
Query q = session.createQuery("select s from Student as s");
List l = q.list();
Student s = null;
Book book = null;
for(int i=0;i<l.size();i++)
{
s = (Student)l.get(i);
System.out.println("姓名: "+s.getSname());
System.out.println("年龄: "+s.getSage());
System.out.println("所有的书:");
Iterator it = s.getBook().iterator();
while(it.hasNext())
{
book = (Book)it.next();
System.out.println("书名: "+book.getBname());
System.out.println("价格: "+book.getBprice());
}
}
}
catch(HibernateException ex){
ex.printStackTrace();
}
finally{
try{
session.close();
}
catch(HibernateException ex2){
}
}
}
public static void main(String[] args)
{
TestOneToMany t = new TestOneToMany();
//t.doCreate();
t.doQuery();
}
}
- hibernate一对多实例
- Hibernate一对多实例
- Hibernate 一对多注解 实例
- 07-hibernate一对多实例
- HIBERNATE一对多配置实例
- Hibernate 一对多注解 实例
- Hibernate一对多(多对一)实例
- Hibernate的一对多关联实例
- 一个HIBERNATE一对多的实例
- 【hibernate实例】项一对多增加查找
- Hibernate一对多关联实例(十七)
- Hibernate一对多双向注解配置实例
- Hibernate 一对多配置实例(inverse)
- hibernate一对多注解开发实例
- Hibernate关系映射----单向一对多实例
- Hibernate 一对多,多对多映射实例
- hibernate :一对多和多对一的配置实例
- Hibernate中一对多的映射关系实例
- C语言标准IO
- 用Android-X86和VirtualBox打造高性能Android开发环境
- 复制剪贴板功能
- virtual、abstract、、Interface区别及应用
- emacs orgmode 文档元数据 控制发布页面显示数据
- Hibernate一对多实例
- 图解oracle 之从客户端到服务端的sql追击
- ThinkPHP定时ajax获取后台数据,使用javascript动态修改前端页面的表格来显示数据
- ubuntu 以管理员身份打开失效
- const几点相当重要的用法
- MySQL 使用 常用命令
- 繁体转简体转半角转拼音
- 使用腾讯微博攻略
- spring 集成 hibernate