简体   繁体   中英

Error while trying to persist and entity with unique constraint for primary key

I am using hibernate and in-memory DB (hsqldb) to persist data. Basically i am writing the Junit for the layer. I am getting an error( user lacks privilege or object not found: CITY ) while using the unique attribute for columns specified as @Id. I wanted to be sure that both the cityId and cityName to be Unique.

@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 the error message

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

When i remove the unique attribute(@ Column) for the cityId the values are getting persisted properly.

Please let me know your comments or any relevant link that will help me understanding the reason behind the same.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM