Hibernate的映射文件assigned异常 使用范例

来源:互联网 发布:知党史作文 编辑:程序博客网 时间:2024/06/05 07:31

 

Hibernate的映射文件assigned异常

你要操作的数据表中的id(即主键)的类型设置成了“自动增长类型”,而在你的 

hibernate.cfg.xml中,id的生成方式是assigned,即 
<id name="id" type="integer"> 
            <column name="id" /> 
            <generator class="assigned" /> 
</id> 
这种搭配是矛盾的! 

主键的assigned生成方式由程序自动生成表的主键,即在你的测试程序中要调用setId()方法,且必 

须在调用save()前调用(或者说在调用save()前必须指定id,其实就是说,主键值不能为空!)。 
把主键的生成方式改为native,它的特征是能够根据底层数据库自动选择主键生成方式。


hibernate手动分配assigned 主键增长方式

 

一、手动分配数据库主键增长方式

assigned

二、还是hibernate_basemapping项目

1、新建 User.class 类

package com.bjsxt.hibernate;

 

import java.util.Date;

 

public class User3 {

    private String id;//实体是有标识的,我们最好建立一个唯一性的标识。

    private String name;

    private String password;

    private Date createTime;

    private Date expireTime;

    public String getId() {

       return id;

    }

    public void setId(String id) {

       this.id = id;

    }

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public String getPassword() {

       return password;

    }

    public void setPassword(String password) {

       this.password = password;

    }

    public Date getCreateTime() {

       return createTime;

    }

    public void setCreateTime(Date createTime) {

       this.createTime = createTime;

    }

    public Date getExpireTime() {

       return expireTime;

    }

    public void setExpireTime(Date expireTime) {

       this.expireTime = expireTime;

    }

 

}

 

2、User3.hbm.xml文件如下:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="com.bjsxt.hibernate.User3" >

       <id name="id" >

           <generator class="assigned"></generator>

       </id>

       <property name="name" unique="true" not-null="true" length="20"/>

       <property name="password" not-null="true" length="10"/>

       <property name="createTime"/>

       <property name="expireTime"/>

    </class>

</hibernate-mapping>

 

3、加入到hibernate.cfg.xml文件

<!DOCTYPE hibernate-configuration PUBLIC

    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

    <session-factory >

         <!-- 数据库改成hibernate_session -->

       <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_basemapping</property>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</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>

 

        <mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>

         <mapping resource="com/bjsxt/hibernate/User2.hbm.xml"/>

         <mapping resource="com/bjsxt/hibernate/User3.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

 

4、执行ExportDB.xml文件,将实体类导出表

5、assigned数据库主键增长方式,主键必须是手动分配的,如果不分配,就出错了。

testSave3()测试方法内容如下:

public void testSave3(){

       Session session=null;

       try{

           session=HibernateUtils.getSession();

           session.beginTransaction();

 

           User3 user3=new User3();

 

           

              user3.setId("001");

           user3.setName("米城粒");

           user3.setPassword("123");

           user3.setCreateTime(new Date());

           user3.setExpireTime(new Date());           

 

           session.save(user3);

           session.getTransaction().commit();

       }catch(Exception e){

           e.printStackTrace();

           session.getTransaction().rollback();

       }finally{

          HibernateUtils.closeSession(session);

       }

 

 

       }

 

三、那么一般情况下,应该采用哪种主键生成策略 呢?

建议采用uuid,因为uuid是由hibernate自动生成的32位的16进制字符串,而不是由数据库生成的,所以它一定比数据库生成方式要快。

可是它也有缺点,就是查找的时候要相对慢些,因为串的查找要比数字的查找慢

 

四、readme.txt文件内容如下:

hibernate基本映射

实体类映射成表

实体类中的普通属性映射成表字段

 

采用<class>标签映射成数据库表,通过<property>标签将普通属性映射成表字段

(普通属性是指不包含自定义类、数组、List、集合等。

 

注意:如果实体类和实体类中的属性和sql中的关键字重复,必须采用table或column重新命名

 

实体类的设计原则:

     *实现一个默认的(即无参数的)构造方法(constructor)

     *提供一个标识属性( identifier property)可选

     *使用非final的类(可选)

         finally不能继承,不能重写,而Lazy方式,是生成一个实体类的子类的,这个子类继承了实体类,所以声明为 final是不可以的。

 

     *为持久化字段声明访问器(accessors)

 

主键生成策略:

    uuid native assigned

 

原创粉丝点击