如何更改hibernate为主键字段插入的默认值?

时间:2013-11-11 21:39:37

标签: java mysql hibernate jpa orm

我有一个名为users的类,其中我有一个主键userid ...现在当我把这个类的对象放在MYSql dataabase中创建一个名为users的表时,Hibernate将一个默认行与主键值为0和其余的字段为空...

当我使用@Column(name =" userid",Columndefintion =" int default -1")时,它仍然将userid的值设为0,这是我不想要的。

USER.Java
package schema;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;



    @Entity
    public class users {

            @Id
            public int userid;

        private String username;
        private String pw;
        private String fname;
        private String lname;
        private String gender;
        private String dob;
        private String jdate;
        private String ldate;
        private String address;
        private String email;
        private String tel;

        @Column(name="pic")
        @Lob
        private byte[] pic;

        @Column(name="tpic")
        @Lob
        private byte[] tpic;

        public int getUserid() {
            return userid;
        }
        public void setUserid(int userid) {
            this.userid = userid;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPw() {
            return pw;
        }
        public void setPw(String pw) {
            this.pw = pw;
        }
        public String getFname() {
            return fname;
        }
        public void setFname(String fname) {
            this.fname = fname;
        }
        public String getLname() {
            return lname;
        }
        public void setLname(String lname) {
            this.lname = lname;
        }
        public String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        public String getDob() {
            return dob;
        }
        public void setDob(String dob) {
            this.dob = dob;
        }
        public String getJdate() {
            return jdate;
        }
        public void setJdate(String jdate) {
            this.jdate = jdate;
        }
        public String getLdate() {
            return ldate;
        }
        public void setLdate(String ldate) {
            this.ldate = ldate;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public String getTel() {
            return tel;
        }
        public void setTel(String tel) {
            this.tel = tel;
        }
        public byte[] getPic() {
            return pic;
        }
        public void setPic(byte[] pic) {
            this.pic = pic;
        }
        public byte[] getTpic() {
            return tpic;
        }
        public void setTpic(byte[] tpic) {
            this.tpic = tpic;
        }


}


Hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as
  ~ indicated by the @author tags or express copyright attribution
  ~ statements applied by the authors.  All third-party contributions are
  ~ distributed under license by Red Hat Inc.
  ~
  ~ This copyrighted material is made available to anyone wishing to use, modify,
  ~ copy, or redistribute it subject to the terms and conditions of the GNU
  ~ Lesser General Public License, as published by the Free Software Foundation.
  ~
  ~ This program is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  ~ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
  ~ for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public License
  ~ along with this distribution; if not, write to:
  ~ Free Software Foundation, Inc.
  ~ 51 Franklin Street, Fifth Floor
  ~ Boston, MA  02110-1301  USA
  -->
<!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>

        <!-- Database connection settings -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/bg</property>
        <property name="connection.username">root</property>
        <property name="connection.password"/>


        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>


        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <mapping class="schema.friendship"/>
        <mapping class="schema.users"/>
        <mapping class="schema.resources"/>
        <mapping class="schema.manipulation"/>



    </session-factory>

</hibernate-configuration>

当我尝试在创建后再次加载数据时,是否有一种方法可以插入不同的默认值coz,当我尝试插入userid = 0时,它表示引起:java.sql.BatchUpdateException:重复条目&#39; 0&#39;关键&#39; PRIMARY&#39;

同样使用hbm2ddl.auto作为更新不会更新现有的0,null,null ...列...为什么?

1 个答案:

答案 0 :(得分:1)

如果您想要的是为每行自动分配给userid列的不同值,则可以使用@GeneratedValue注释,例如:

@Id @GeneratedValue(strategy=GenerationType.TABLE)
public int userid;

有关可用的不同生成策略,请参阅JPA / Hibernate文档。