Java 如何更改hibernate为主键字段插入的默认值?
我有一个名为users的类,其中我有一个主键userid。。。现在,当我将这个类的对象放在MYSql Database中创建一个名为users的表时,Hibernate将一个主键值为0的默认行,其余字段为null 当我使用@Column(name=“userid”,columnDefinition=“int default-1”)时,它仍然将userid的值设置为0,这是我不想要的Java 如何更改hibernate为主键字段插入的默认值?,java,mysql,hibernate,jpa,orm,Java,Mysql,Hibernate,Jpa,Orm,我有一个名为users的类,其中我有一个主键userid。。。现在,当我将这个类的对象放在MYSql Database中创建一个名为users的表时,Hibernate将一个主键值为0的默认行,其余字段为null 当我使用@Column(name=“userid”,columnDefinition=“int default-1”)时,它仍然将userid的值设置为0,这是我不想要的 USER.Java package schema; import javax.persistence.Colum
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>
USER.Java
包模式;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.Id;
导入javax.persistence.Lob;
导入javax.persistence.Table;
@实体
公共类用户{
@身份证
公共int用户id;
私有字符串用户名;
私有字符串pw;
私有字符串fname;
私有字符串名称;
私人字符串性别;
私有字符串dob;
私有字符串jdate;
私有字符串ldate;
私有字符串地址;
私人字符串电子邮件;
私人电话;
@列(name=“pic”)
@高球
私有字节[]pic;
@列(name=“tpic”)
@高球
专用字节[]tpic;
public int getUserid(){
返回用户标识;
}
public void setUserid(int userid){
this.userid=userid;
}
公共字符串getUsername(){
返回用户名;
}
public void setUsername(字符串用户名){
this.username=用户名;
}
公共字符串getPw(){
返回pw;
}
公共无效设置pw(字符串pw){
this.pw=pw;
}
公共字符串getFname(){
返回fname;
}
公共void setFname(字符串fname){
this.fname=fname;
}
公共字符串getLname(){
返回lname;
}
公共void setLname(字符串lname){
this.lname=lname;
}
公共字符串getGender(){
返回性别;
}
公共无效设置性别(字符串性别){
这个。性别=性别;
}
公共字符串getDob(){
返回dob;
}
公共void setDob(字符串dob){
this.dob=dob;
}
公共字符串getJdate(){
返回jdate;
}
公共void setJdate(字符串jdate){
this.jdate=jdate;
}
公共字符串getLdate(){
返回ldate;
}
公共void setLdate(字符串ldate){
this.ldate=ldate;
}
公共字符串getAddress(){
回信地址;
}
公共无效设置地址(字符串地址){
this.address=地址;
}
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getTel(){
返回电话;
}
公共电话(字符串电话){
this.tel=tel;
}
公共字节[]getPic(){
返回pic;
}
公共void setPic(字节[]pic){
this.pic=pic;
}
公共字节[]getTpic(){
返回tpic;
}
公共void setTpic(字节[]tpic){
this.tpic=tpic;
}
}
Hibernate.cfg.xml
org.hibernate.dialogue.mysqldialogue
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/bg
根
1.
真的
更新
是否有一种方法可以插入不同的默认值,因为当我在创建后再次尝试加载数据时,当我尝试插入userid=0时,它说是由以下原因引起的:java.sql.BatchUpdateException:键“PRIMARY”的重复条目“0”
另外,使用hbm2ddl.auto作为更新不会更新现有的0、null、null。。。专栏。。为什么?如果您希望为每一行的userid列自动分配不同的值,您可以使用
@GeneratedValue
注释,例如:
@Id @GeneratedValue(strategy=GenerationType.TABLE)
public int userid;
请参阅JPA/Hibernate文档以了解可用的不同生成策略。您是否尝试过在java构造函数/初始值设定项上设置默认值,例如:
public int userid=-1代码>?这很有效。。我很感激。。但是有没有一种方法可以删除数据库中的所有现有数据,然后插入所有新数据或。。。替换现有行,例如:我的userid=0,rest字段为null。。现在,当我想插入userid=0和具有某些值的rest字段时。。有办法吗?除非您告诉Hibernate ID是自动生成的,否则Hibernate会插入字段所包含的内容,因此默认为0。阅读hibernate文档中的ID生成。但是有没有一种方法可以删除数据库中的所有现有数据,然后插入所有新数据或。。。替换现有行,例如:我的userid=0,rest字段为null。。现在,当我想插入userid=0和具有某些值的rest字段时。。有办法吗?使用您喜欢的数据库浏览工具,执行delete from user
。在使用Hibernate之前,您最好先学习SQL的基础知识。您还可以尝试将hbm2ddl.auto
设置为create
或create drop
。