一、TestStudent.hbm.xml
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="com.sdkj.hibernate.domain"> 6 7 <class table="t_student1" name="Test_Student"> 8 <id name="id"> 9 <column name="id"></column> 10 <generator class="native"></generator> 11 </id> 12 <property name="name" column="name"></property> 13 14 <!-- 组件标签 --> 15 <component name="zj_studentInfo" class="ZJ_StudentInfo"> 16 <property name="sex" column="sex"></property> 17 <property name="classes" column="classes"/> 18 <property name="address" column="address"></property> 19 <property name="birthday" column="birthday" type="date"></property> 20 </component> 21 </class> 22 </hibernate-mapping>
二、TestZuJIan
1 package com.sdkj.hibernate.controller; 2 3 import com.sdkj.hibernate.domain.Test_Student; 4 import com.sdkj.hibernate.domain.ZJ_StudentInfo; 5 import com.sdkj.hibernate.util.CommonUtil; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.Transaction; 9 import org.junit.jupiter.api.Test; 10 11 import java.util.Date; 12 13 /** 14 * @Author wangshuo 15 * @Date 2022/4/9, 13:23 16 * Please add a comment 17 */ 18 public class TestZuJian extends CommonUtil { 19 20 @Test 21 public void test(){ 22 23 SessionFactory sessionFactory = getSessionFactory(); 24 Session session = sessionFactory.openSession(); 25 Transaction transaction = session.beginTransaction(); 26 27 Test_Student test_student = new Test_Student(); 28 test_student.setName("王硕"); 29 test_student.setZj_studentInfo(new ZJ_StudentInfo("男","China","3",new Date())); 30 31 session.save(test_student); 32 33 commit(transaction,sessionFactory,session); 34 } 35 }
本文来自博客园,作者:荣慕平,转载请注明原文链接:https://www.cnblogs.com/rongmuping/articles/16121720.html