1、分为三部分 ①基础配置参数使用 ②多对一映射 ③一对多映射
2、第一点中说的三部分,分别对应包:
①com.demo.sshtest ②com.demossh2 ③com.demo.sshtest3
3、对应代码
————————第一部分 start————————
直接上代码,注释都写到Info.hbm.xml里面
1、Info.java 模型类
package com.demo.sshtest;
import java.util.Date;
import java.sql.Blob;
public class Info {
public Integer id;
public String name;
public String pswd;
public String description;
public Date date;
//长文本
public String content;
//二进制数据
public Blob image;
public DetailInfo detailInfo;
public DetailInfo getDetailInfo() {
return detailInfo;
}
public void setDetailInfo(DetailInfo detailInfo) {
this.detailInfo = detailInfo;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Blob getImage() {
return image;
}
public void setImage(Blob image) {
this.image = image;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPswd() {
return pswd;
}
public void setPswd(String pswd) {
this.pswd = pswd;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Info(){}
public Info(String name, String pswd, String description) {
super();
this.name = name;
this.pswd = pswd;
this.description = description;
}
public Info(Integer id,String name, String pswd, String description) {
super();
this.id = id;
this.name = name;
this.pswd = pswd;
this.description = description;
}
@Override
public String toString() {
return "Info [id=" + id + ", name=" + name + ", pswd=" + pswd + ", description=" + description + "]";
}
}
2、InfoDeatil.java Info的子模型类
package com.demo.sshtest;
public class DetailInfo {
public String firstName;
public String lastName;
public String age;
public Info info;
public Info getInfo() {
return info;
}
public void setInfo(Info info) {
this.info = info;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public DetailInfo(){}
@Override
public String toString() {
return "DetailInfo [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + ", info=" + info
+ "]";
}
}
数据表InfoDetail的外键是Info的id参数
3、TestHBMconfig.java 测试效果用的类
package com.demo.sshtest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Date;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.sql.Blob;
public class TestHBMconfig {
public SessionFactory sessionFactory;
public Session session;
public Transaction transaction;
@Before
public void init(){
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
System.out.println("init");
}
@After
public void destory(){
transaction.commit();
session.close();
sessionFactory.close();
System.out.println("destory");
}
@Test
public void test() throws Exception{
System.out.println("test");
Info info = new Info();
DetailInfo detailInfo = new DetailInfo();
info.setName("test");
info.setPswd("testtest");
info