<many to one>和<one to many>
2013-12-22 17:33
369 查看
一对多,多对一(双向关联)
1、PO
//User类
public class User {
private int userId;
private String userName;
//在一的一方要引用多的一方的类,并把它放入到Set集合中
private Set<Video> videos = new HashSet<Video>();
public User() {
super();
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
//Video类
public class Video {
private int videoId;
private String title;
//在多的一方要引用一的一方
private User user;
public Video() {
super();
}
public int getVideoId() {
return videoId;
}
public void setVideoId(int videoId) {
this.videoId = videoId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
2、配置文件
//User的配置文件
<hibernate-mapping
package="com.puckasoft.po">
<class name="User" table="user">
<id name="userId" column="user_id">
<generator class="native"></generator>
</id>
<property name="userName" column="user_name"></property>
<!-- 在一的一方要设置set,和PO里的Set集合相对应,外键就是一方的id -->
<set name="videos" cascade="save-update" inverse="true">
<key column="user_id"></key>
<one-to-many class="Video" />
</set>
</class>
</hibernate-mapping>
//Video的配置文件
<hibernate-mapping
package="com.puckasoft.po">
<class name="Video" table="video">
<id name="videoId" column="video_id">
<generator class="native"></generator>
</id>
<property name="title" column="title"></property>
<!-- 多的一方设置<many-to-one> -->
<many-to-one name="user" column="user_id"></many-to-one>
</class>
</hibernate-mapping>
3、配置hibernate.cfg.xml文件
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://127.0.0.1:3306/hibernate
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping resource="com/puckasoft/po/User.hbm.xml" />
<mapping resource="com/puckasoft/po/Video.hbm.xml" />
</session-factory>
</hibernate-configuration>
4、HibernateUtil类
public class HibernateUtil {
private static SessionFactory factory;
static{
try{
factory = new Configuration().configure().buildSessionFactory();
} catch(HibernateException e){
e.printStackTrace();
System.out.println("sessionFactory 创建失败");
throw new ExceptionInInitializerError();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static void closeSessionFactory() {
if(factory!=null){
factory.close();
}
}
public static Session getSession(){
return factory.openSession();
}
public static void closeSession(Session session){
if(session!=null){
session.close();
}
}
}
5、导表
public class ExportTable extends TestCase {
Configuration configuration;
protected void setUp() throws Exception {
System.out.println("invoke setUp");
configuration = new Configuration().configure();
}
protected void tearDown() throws Exception {
System.out.println("invoke tearDown");
}
public void testExport() throws Exception {
// new Configuration() 加载 classpath 下的 hibernate.properties 文件
// 加载 classpath 下的 hibernate.cfg.xml 文件
Configuration configuration = new Configuration().configure();
SchemaExport export = new SchemaExport(configuration);
export.create(true, true);
}
}
1、PO
//User类
public class User {
private int userId;
private String userName;
//在一的一方要引用多的一方的类,并把它放入到Set集合中
private Set<Video> videos = new HashSet<Video>();
public User() {
super();
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
//Video类
public class Video {
private int videoId;
private String title;
//在多的一方要引用一的一方
private User user;
public Video() {
super();
}
public int getVideoId() {
return videoId;
}
public void setVideoId(int videoId) {
this.videoId = videoId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
2、配置文件
//User的配置文件
<hibernate-mapping
package="com.puckasoft.po">
<class name="User" table="user">
<id name="userId" column="user_id">
<generator class="native"></generator>
</id>
<property name="userName" column="user_name"></property>
<!-- 在一的一方要设置set,和PO里的Set集合相对应,外键就是一方的id -->
<set name="videos" cascade="save-update" inverse="true">
<key column="user_id"></key>
<one-to-many class="Video" />
</set>
</class>
</hibernate-mapping>
//Video的配置文件
<hibernate-mapping
package="com.puckasoft.po">
<class name="Video" table="video">
<id name="videoId" column="video_id">
<generator class="native"></generator>
</id>
<property name="title" column="title"></property>
<!-- 多的一方设置<many-to-one> -->
<many-to-one name="user" column="user_id"></many-to-one>
</class>
</hibernate-mapping>
3、配置hibernate.cfg.xml文件
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://127.0.0.1:3306/hibernate
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping resource="com/puckasoft/po/User.hbm.xml" />
<mapping resource="com/puckasoft/po/Video.hbm.xml" />
</session-factory>
</hibernate-configuration>
4、HibernateUtil类
public class HibernateUtil {
private static SessionFactory factory;
static{
try{
factory = new Configuration().configure().buildSessionFactory();
} catch(HibernateException e){
e.printStackTrace();
System.out.println("sessionFactory 创建失败");
throw new ExceptionInInitializerError();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static void closeSessionFactory() {
if(factory!=null){
factory.close();
}
}
public static Session getSession(){
return factory.openSession();
}
public static void closeSession(Session session){
if(session!=null){
session.close();
}
}
}
5、导表
public class ExportTable extends TestCase {
Configuration configuration;
protected void setUp() throws Exception {
System.out.println("invoke setUp");
configuration = new Configuration().configure();
}
protected void tearDown() throws Exception {
System.out.println("invoke tearDown");
}
public void testExport() throws Exception {
// new Configuration() 加载 classpath 下的 hibernate.properties 文件
// 加载 classpath 下的 hibernate.cfg.xml 文件
Configuration configuration = new Configuration().configure();
SchemaExport export = new SchemaExport(configuration);
export.create(true, true);
}
}
相关文章推荐
- <many-to-one>设置
- 数据库设计<many-to-one>和<one-to-many>双向映射
- <三>重温Hibernate one to many 映射配置
- Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
- <unable to get file lock, will retry ...>进程重复启动
- hibernate中hbm.xml<many-to-one><one-to-many>中一些属性详解
- <二>重温Hibernate one to one 映射
- <Introduction to Java Programming> Note 01
- <BEA-141288> <Unable to obtain file lock but will retry.>
- Hibernate——<many-to-one/>
- <iOS> The operation could not be performed because of one or more tree conflicts.
- <<Beginning XML with DOM and Ajax From Novice to Professiona>>Contents
- <LeetCode OJ> 109 / 108 Convert Sorted ( List / Array ) to Binary Search Tree
- 020 <one-to-one>、<many-to-one>单端关联上的lazy(懒加载)属性
- <Feb 28, 2015 3:21:18 PM CST> <Info> <Management> <BEA-141281> <unable to get file lock, will retry
- <the hacker guide to python>读书笔记
- Hibernate——<one-to-many/>
- <Android> Failed to pull selection 解决
- <Introduction to modern Cryptogrsphy>读书笔记
- <transmission> element is used to link actuators to joints