我有一个类ABC
,其中包含id
类型的变量uuid
并且是主键。我使用postgres保存对象,而我的数据库id
的类型为uuid
。当我执行此代码时,我收到以下错误。
@Entity
@Table(name = "event", schema = "public", uniqueConstraints = @UniqueConstraint(columnNames = "id"))
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class ABC implements java.io.Serializable {
@Type(type = "pg-uuid")
private UUID id;
@GenericGenerator(name = "uuid", strategy = "uuid")
@Id
@GeneratedValue(generator = "uuid")
@Column(name = "id", unique = true, nullable = false)
@Type(type = "pg-uuid")
public UUID getId() {
return this.id;
}
public void setId(UUID id) {
this.id = id;
}
}
使用主类使用hibernate在postgres中保存ABC的对象:
ABCDAO dao = new ABCDAO();
ABC be = new ABC();
Session session = dao.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(be);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
错误:
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.istarindia.apps.dao.IstarEvent.id
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:119)
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.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:772)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:746)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:750)
at istar.TestDB.main(TestDB.java:105)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:65)
... 11 more
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.istarindia.apps.dao.IstarEvent.id
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:119)
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.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:772)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:746)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:750)
at istar.TestDB.main(TestDB.java:105)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:65)
... 11 more
答案 0 :(得分:0)
解决了问题,改变了这样的getter,setter和注释
@javax.persistence.Id
@GenericGenerator(name = "uuid-gen", strategy = "uuid2")
@GeneratedValue(generator = "uuid-gen")
@org.hibernate.annotations.Type(type="pg-uuid")
public UUID getId() {
return id;
}
public void setId(UUID value) {
this.id = value;
}