尝试持久化时出错,并且主键具有唯一约束的实体

时间:2013-11-12 04:46:19

标签: java database hibernate jpa

我正在使用hibernate和内存数据库(hsqldb)来保存数据。基本上我正在为图层编写Junit。 我在使用指定为@Id的列的unique属性时收到错误(用户缺少权限或找不到对象: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的唯一属性(@ Column)时,值将被正确保存。

请告诉我您的意见或任何相关链接,以帮助我了解背后的原因。

1 个答案:

答案 0 :(得分: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