<many to one>和<one to many>

一对多,多对一(双向关联)

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);
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值