Hibernate一点理解

本文深入解析Hibernate中xx.hbm.xml配置文件的作用,通过具体代码示例展示了如何正确配置对象到数据库表的映射关系,强调了配置文件的重要性及常见错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hibernate中xx.hbm.xml配置文件的作用:
        配置文件只是负责被Hibernate框架读取,作为一种ORM类型框架,可想而知,负责Object到DB的一种映射。那么在框架中配置的文件肯定是被框架抓取,并来组织数据到数据库中的插入操作,所以映射配置文件的错误将导致数据不能够正确插入数据库。
        还有一种情况,即使映射的配置文件配置成功,但你的程序没有按照你配置的方式来组织代码,也不会往数据库中插入正确的数据。
下面给出代码对上面的解释来进行说明:
--------------------------------->User.java/Profile.java
public class User implements java.io.Serializable {
 private Integer id;
 private String username;
 private String password;
 private Profile profile;
 setter and getter methods......
}
public class Profile {
 private String email;
 private String mobile;
 private String phone;
 private String address;
 private String postcode;
setter and getter methods......
}
--------------------------->User.hbm.xml
<hibernate-mapping>
 <class name="com.digilover.hibernate.models.User" table="USERS"
  schema="SCOTT" lazy="true">
  <id name="id" type="java.lang.Integer">
   <column name="ID" precision="10" scale="0" />
   <generator class="increment"></generator>
  </id>
  <property name="username" type="java.lang.String">
   <column name="USERNAME" length="20" />
  </property>
  <property name="password" type="java.lang.String">
   <column name="PASSWORD" length="20" />
  </property>
   <component name="profile" class="com.digilover.hibernate.models.Profile">
   <property name="email" type="java.lang.String">
    <column name="EMAIL" length="200" />
   </property>
   <property name="mobile" type="java.lang.String">
    <column name="MOBILE" length="11" />
   </property>
   <property name="phone" type="java.lang.String">
    <column name="PHONE" length="20" />
   </property>
   <property name="address" type="java.lang.String">
    <column name="ADDRESS" length="200" />
   </property>
   <property name="postcode" type="java.lang.String">
    <column name="POSTCODE" length="20" />
   </property>
   </component>
 </class>
</hibernate-mapping>
-------------------------->Test.java
public class Test {
 public static void main(String[] args) {
  User user = new User();
  user.setUsername("朱茵");
  user.setPassword("222222");
  
  Profile profile = new Profile();
  profile.setEmail(" zhuyin@sina.cn");
  profile.setPostcode("222222");
  profile.setAddress("北京海定区王府井路2");
  profile.setMobile("12345678909");
  profile.setPhone("7654321");
  
  //user.setProfile(profile);
   save(user);
 }
public static void save(User user){
  Session session = HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
  session.beginTransaction();
  session.save(user);
  session.getTransaction().commit();
 }
 
}
注意上面注释掉的那句话,如果没有了它,那么数据库中不会正确插入完整数据,Profile中的属性值不会被插入数据库中,因为Profile对象profile没有与User对象user关联,框架根据映射的配置文件找不到与User对象关联的Profile对象。这就是我上面所说的映射文件配置正确,但程序代码组织的不到位,导致框架不能将实体类ORM到数据库中。至于第一种情况,可以将上面的红色字体配置去掉,试验下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值