0

我正在使用休眠和内存数据库(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 的唯一属性(@ Column)时,值会正确保留。

请让我知道您的评论或任何相关链接,以帮助我理解其背后的原因。

4

1 回答 1

1

When you declare a column as @Id in Hibernate it is already assumed to be unique. SO according to me you do not need the unique attribute.

You can have a look at the Hibernate Docs

Also since you are using @GeneratedValue please make sure you have handled the creation of the keys correctly

If you want to set it to auto you can do it like this

@GeneratedValue(strategy = GenerationType.AUTO)

If you have a database sequence which you wish to use then you can set it like this

@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;
}

Please refer to the following link

于 2013-11-12T05:28:15.823 回答