首页 技术 正文
技术 2022年11月19日
0 收藏 933 点赞 4,141 浏览 4279 个字

1.为什么使用component组件?

当一个表的列数目比较多时,可以根据属性分类,将一个java对象拆分为几个对象。

数据库还是一张表,不过有多个对象与之对应。

2.实例

2.1 Java 对象:

 public class Person {     private int id;
private Name name;
private Parents parents; public Parents getParents() {
return parents;
} public void setParents(Parents parents) {
this.parents = parents;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public Name getName() {
return name;
} public void setName(Name name) {
this.name = name;
}
}
 public class Parents {
private Name father;
private Name mother; public Name getFather() {
return father;
} public void setFather(Name father) {
this.father = father;
} public Name getMother() {
return mother;
} public void setMother(Name mother) {
this.mother = mother;
}
}
 public class Name {
private String bigName;
private String nickName;
private int nameVersion; public String getBigName() {
return bigName;
} public void setBigName(String bigName) {
this.bigName = bigName;
} public int getNameVersion() {
return nameVersion;
} public void setNameVersion(int nameVersion) {
this.nameVersion = nameVersion;
} public String getNickName() {
return nickName;
} public void setNickName(String nickName) {
this.nickName = nickName;
}
}

2.2 Hibernate 配置文件 hibernate.cfg.xml

 <?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"
>
<hibernate-configuration>
<session-factory>
<!-- Platform settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Miscellaneous Settings -->
<property name="show_sql">true</property>
<!--mapping files-->
<mapping resource="test/com/hibernate/config/hbm/person.hbm.xml"/>
</session-factory> </hibernate-configuration>

映射文件person.hbm.xml

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="test.com.hibernate.component.Person" table="person" lazy="false">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<component name="name" class="test.com.hibernate.component.Name" >
<property name="bigName" column="bigName" type="string"/>
<property name="nickName" column="nickName" type="string"/>
<property name="nameVersion" column="nameVersion" type="integer"/>
</component>
<component name="parents" class="test.com.hibernate.component.Parents">
<component name="father" class="test.com.hibernate.component.Name">
<property name="bigName" column="f_bigName" type="string"/>
<property name="nickName" column="f_nickName" type="string"/>
<property name="nameVersion" column="f_nameVersion" type="integer"/>
</component>
<component name="mother" class="test.com.hibernate.component.Name">
<property name="bigName" column="m_bigName" type="string"/>
<property name="nickName" column="m_nickName" type="string"/>
<property name="nameVersion" column="m_nameVersion" type="integer"/>
</component>
</component>
</class>
</hibernate-mapping>

2.3 Hibernate 操作代码:

HibernateUtil.java

 public class HibernateUtil {
static SessionFactory sessionFactory = null;
static{
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure("test/com/hibernate/config/hibernate.cfg.xml").build();
MetadataSources sources = new MetadataSources();
Metadata metadata = sources.buildMetadata(registry);
sessionFactory = metadata.buildSessionFactory();
}
public static Session openSession(){
if(sessionFactory==null){
throw new HibernateException("session factory is null!");
}
return sessionFactory.openSession();
}
public static void destroy() {
if(sessionFactory!=null){
sessionFactory.close();
}
}
}

Test.java

 public class Test {     public static void main(String[] args) {         Name name = new Name();
name.setBigName("Daughter");
name.setNickName("girl");
name.setNameVersion(1);
Person person = new Person();
person.setName(name); Name father = new Name();
father.setBigName("Father");
father.setNickName("man");
father.setNameVersion(1);
Name mother = new Name();
mother.setBigName("Mother");
mother.setNickName("woman");
mother.setNameVersion(1); Parents parents = new Parents();
parents.setFather(father);
parents.setMother(mother); person.setParents(parents); PersonDao.save(person);
}
}

上述例子使用Hibernate版本为5.2Final,配置文件也可以用以前的Configuration配置。

2.4 运行结果

自动创建一个person表,并插入一条数据

hibernate  hibernate.cfg.xml  component 组件

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894