如何在Hibernate中定义非生成的主键字段

时间:2014-04-11 08:46:52

标签: java mysql sql hibernate primary-key

考虑班级人物:

package model;
import java.io.Serializable;
import openAPI.modelInterfaces.PersonInterface;


/**
 * 
 * @author X3
 *
 */
public class Person implements PersonInterface , Serializable
{

    private static final long serialVersionUID = 1L;

    private String m_id = "Missing ID number";
    private String m_firstName = "Missing first name";;
    private String m_lastName = "Missing last name";;
    private String m_userName = "Missing user name";;
    private String m_password = "Missing password";
    private String m_address = "Missing address";
    private String m_status = "Missing status";

    /**
     * 
     * @param _id
     * @param _firstName
     * @param _lastName
     * @param _userName
     * @param _password
     * @param _address
     */
    public Person(String _id, String _firstName , String _lastName , 
            String _userName , String _password , String _address , String _status)
    {
        this.m_id = _id;
        this.m_firstName = _firstName;
        this.m_lastName = _lastName;
        this.m_userName = _userName;
        this.m_password = _password;
        this.m_address = _address;
        this.m_status = _status;
    }

    public Person() {}


    //////////////////////////  getters /////////////////////////


    @Override
    public String getLastName() {

        return this.m_lastName;
    }


    @Override
    public String getIdnumber() {
        return this.m_id;
    }



    @Override
    public String getFirstName() {
        return this.m_firstName;
    }



    @Override
    public String getUserName() {
        return this.m_userName;
    }




    @Override
    public String getPassword() {
        return this.m_password;
    }


    @Override
    public String getAddress() {
        return this.m_address;
    }


    ///////////////////// setters ///////////////////////

    @Override
    public void setAddress(String _address) {

        if (!isMissing(_address)) 
            this.m_address = _address;
    }

    @Override
    public void setLastName(String _lastName) {

        if (!isMissing(_lastName)) 
            this.m_lastName = _lastName;
    }

    @Override
    public void setPassword(String _password) 
    {
        if (!isMissing(_password)) 
            this.m_password = _password;
    }


    @Override
    public void setUsername(String _userName) {

            this.m_userName = _userName;
    }


    @Override
    public void setFirstName(String _newName) {

        if (!isMissing(_newName)) 
            this.m_firstName = _newName;
    }


    @Override
    public void setidNumber(String _newID) {

        if (!isMissing(_newID)) 
            this.m_id = _newID;
    }


    // others 

    private boolean isMissing(String value) 
    {
        return((value == null) || (value.trim().equals("")));
    }

    @Override
    public void setStatus(String _status) {
        if (!isMissing(_status)) 
            this.m_status = _status;

    }

    @Override
    public String getStatus() {
        return this.m_status;
    }
}

它在MYSQL中的匹配表:

/**
 * creating a table for each type person in the bank 
 * @param tableType
 * @throws SQLException
 */
public void createTable(String tableType) throws SQLException
{
    m_statement.executeUpdate (

            "CREATE TABLE IF NOT EXISTS "+ tableType +" ("
            + "FirstName CHAR(20), LastName CHAR(20),"
            + "Address CHAR(50), IdNumber CHAR(20),"
            + "UserName CHAR(20), Password CHAR(20) , Status CHAR(20) , PRIMARY KEY (IdNumber))");
}

我想使用Hibernate将Person对象映射到MYSQL DB中的记录。

这是hbm.xml文件:

<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!-- name of the package -->
<hibernate-mapping package="model">

<!-- name of the class & name of the table , that we want to MAP the object to  -->
<class name="Person" table="PersonnelTable">

<id name="m_id" type="String" column="IdNumber" >
<generator class="identity"/>
</id>

<property name="m_firstName" column="FirstName" type="string"/>
<property name="m_lastName" column="LastName" type="string"/>
<property name="m_userName" column="UserName" type="string"/>
<property name="m_passWord" column="Password" type="string"/>
<property name="m_address" column="Address" type="string"/>
<property name="m_status" column="PersonType" type="string"/>

</class>
</hibernate-mapping>

如何将主键privateId设置为非自动增量主键?

我不希望MYSQL为我映射的每个人自动生成一个唯一的密钥,因为 ID由用户给出。

谢谢

1 个答案:

答案 0 :(得分:1)

免费提供,易于搜索的documentation说:

  

分配

     

允许应用程序在调用save()之前为对象分配标识符。如果未指定任何元素,则这是默认策略。

此外,您绝对不应使用char作为列的类型,尤其是ID。 char列用空格填充。请改用varchar。

并且:自10年前Java 5以来存在注释。为什么你仍然使用专有的XML文件而不是标准的,更容易和更安全的JPA注释来定义你的映射?