简体   繁体   English

如何更改休眠为“主键”字段插入的默认值?

[英]How to change the default value that hibernate inserts for Primary key field?

I have class called users, in which I have a primary key userid ... Now when I put the object of this class in MYSql dataabase which creates a table named users, Hibernate puts a default row with primary key having value 0 and rest of the fields as null... 我有一个名为users的类,其中有一个主键userid ...现在,当我将该类的对象放入创建表名为users的表的MYSql数据库中时,Hibernate放置一个默认行,其主键的值为0,其余部分为字段为空...

When I use @Column(name="userid", Columndefintion="int default -1") it still puts the value for userid as 0 which I don`t want. 当我使用@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>

Is there a way I can insert a different default value coz when I try to load data again after creation, when I try to insert userid=0 it says Caused by: java.sql.BatchUpdateException: Duplicate entry '0' for key 'PRIMARY' 当我尝试在创建后再次加载数据时,是否有一种方法可以插入不同的默认值coz,当我尝试插入userid = 0时,它说:原因:java.sql.BatchUpdateException:键“ PRIMARY”的条目“ 0”重复'

Also using hbm2ddl.auto as update does not update that existing 0,null,null ... column.. why? 还使用hbm2ddl.auto作为更新不会更新现有的0,null,null ...列..为什么?

If what you want is to have a different value automatically assigned to the userid column for each row, you can use the @GeneratedValue annotation, eg: 如果要为每行的userid列自动分配一个不同的值,则可以使用@GeneratedValue批注,例如:

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

See the JPA/Hibernate doc for the different generation strategies available. 有关可用的不同生成策略,请参见JPA / Hibernate文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字段 'index_id' 没有默认值”在尝试使用 primary_key 作为 Hibernate 中的复合键的一部分时 - Field 'index_id' doesn't have a default value" when trying to use the primary_key as part of composite key in Hibernate 休眠主外键字段 - Hibernate Primary Foreign Key field Hibernate通过字段+ max的组合在非主键上生成值 - Hibernate generate value on non primary key with combination of field + max 如何在Hibernate中定义未生成的主键字段 - How to define a non generated primary key field in Hibernate 如何使用Java + Hibernate在SQL Server的主键中插入值? - How to insert value into primary key in SQL server by using java + hibernate? 休眠映射字段作为主键和外键 - Hibernate mapping field as a primary and foreign key JPA-如何使用表默认值生成主键? - JPA - How to use table default value for primary key generation? 如何使用自动生成的值制作复合主键,并使用休眠和Spring MVC制作一个外键 - How make a composite primary key using auto generated value and one foreign key using hibernate and spring MVC 如何通过Hibernate合并为字段设置默认NULL值 - How to set default NULL value for a field via Hibernate merge Hibernate Envers-如何搜索字符串主键? - Hibernate Envers - How to search for string primary key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM