尝试持久化和具有主键唯一约束的实体时出错
Posted
技术标签:
【中文标题】尝试持久化和具有主键唯一约束的实体时出错【英文标题】:Error while trying to persist and entity with unique constraint for primary key 【发布时间】:2013-11-12 04:46:18 【问题描述】:我正在使用休眠和内存数据库 (hsqldb) 来保存数据。基本上我正在为该层编写Junit。 我在使用指定为@Id 的列的唯一属性时收到错误(用户缺少权限或找不到对象:CITY)。我想确保 cityId 和 cityName 都是唯一的。
@Entity
@Table(name="CITY")
public class City
@Id
@GeneratedValue
@Column(name="CITY_ID",unique= true)
private long id;
@Column(name="CITY_NAME", nullable=false, unique= true)
private String name;
PFB 错误信息
org.hibernate.exception.SQLGrammarException: could not insert: [model.City]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
at com.loiane.dao.CityDAO.saveCity(CityDAO.java:39)
at com.loiane.main.Main.main(Main.java:18)
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: CITY
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:524)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:116)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.prepare(IdentityGenerator.java:90)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:54)
... 17 more
当我删除 cityId 的 unique 属性(@ Column)时,值会正确保留。
请让我知道您的 cmets 或任何相关链接,以帮助我了解其背后的原因。
【问题讨论】:
【参考方案1】:当您在 Hibernate 中将列声明为 @Id 时,它已被假定为唯一的。所以根据我的说法,你不需要唯一属性。
你可以看看Hibernate Docs
此外,由于您使用的是@GeneratedValue,请确保您已正确处理密钥的创建
如果你想将它设置为自动,你可以这样做
@GeneratedValue(strategy = GenerationType.AUTO)
如果你有一个想要使用的数据库序列,那么你可以这样设置它
@Entity
// Define a sequence - might also be in another class:
@SequenceGenerator(name="seq", initialValue=1, allocationSize=100)
public class EntityWithSequenceId
// Use the sequence that is defined above:
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
@Id long id;
请参考以下link
【讨论】:
以上是关于尝试持久化和具有主键唯一约束的实体时出错的主要内容,如果未能解决你的问题,请参考以下文章