1. 程式人生 > >hibernate 執行save方法報錯

hibernate 執行save方法報錯

WARN: HHH000409: Using org.hibernate.id.UUIDHexGenerator which does not generate IETF RFC 4122 compliant UUID values; consider using org.hibernate.id.UUIDGenerator instead
Exception in thread "main" org.hibernate.PropertyAccessException: could not set a field value by reflection setter of org.hibernate.Student.number
at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:151)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setIdentifier(AbstractEntityTuplizer.java:384)
at org.hibernate.persister.entity.AbstractEntityPersister.setIdentifier(AbstractEntityPersister.java:4555)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:135)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:683)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:675)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
at org.hibernate.Main.save(Main.java:76)
at org.hibernate.Main.main(Main.java:24)
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Integer field org.hibernate.Student.number to java.lang.String
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
at java.lang.reflect.Field.set(Unknown Source)
at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:139)
... 14 more

實體類student:

package org.hibernate;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Entity;
import org.hibernate.annotations.GenericGenerator;

@Entity //將一個類宣告為一個實體bean(即一個持久化POJO類) 
@Table(name="student") //註解聲明瞭該實體bean對映指定的表(student)
public class Student {
@Id //用來註冊主屬性,@GeneratedValue用來註冊主屬性的生成策略,@Column用來註冊屬性,@Version用來註冊樂觀鎖  
@GenericGenerator(name = "uuidGen" , strategy = "uuid")
@GeneratedValue(generator="uuidGen")
@Column(name="number")
public Integer number;

@Column(name="name")
public String name;

@Column(name="age")
public Integer age ;

@Column(name="sex")
public String sex;

@Column(name="loginname")
public String loginname;

@Column(name="pwd")
public String pwd;

public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}

save方法:

public static void save(Session session){
Student student=new Student();
student.setNumber(10);
student.setLoginname("nihao");
student.setPwd("123456");
student.setName("nihao");
student.setAge(21);
student.setSex("男");
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
}

解決方案:刪除實體類中的註解

@GenericGenerator(name = "uuidGen" , strategy = "uuid")
@GeneratedValue(generator="uuidGen")