Good morning everyone
大家,早安
I am using HSQLDB with Hibernate. I have a situation where even if I set the primary key value, I am still getting an integrity constraint violation: NOT NULL check constraint; SYS_CT_10346 table: NUMBERS column: PK
. As can be seen below.
我正在使用HSQLDB和Hibernate。我有一种情况,即使我设置主键值,我仍然得到完整性约束违规:NOT NULL检查约束; SYS_CT_10346表:NUMBERS列:PK。如下所示。
SEVERE: integrity constraint violation: NOT NULL check constraint; SYS_CT_10346 table: NUMBERS column: PK
org.hibernate.exception.ConstraintViolationException: could not insert: [entity.Numbers]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at dbconn.NumbersDB.saveNumbers(NumbersDB.java:75)
at gui.UI.saveNumberStateButtonActionPerformed(UI.java:1807)
at gui.UI.access$1800(UI.java:39)
at gui.UI$20.actionPerformed(UI.java:788)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10346 table: NUMBERS column: PK
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 55 more
Here is my mapping
这是我的映射
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 22 Feb 2016 9:01:34 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="entity.Numbers" table="NUMBERS" schema="PUBLIC" catalog="PUBLIC">
<id name="pk" type="int">
<column name="PK" />
<generator class="native" />
</id>
<many-to-one name="account" class="entity.Account" fetch="select">
<column name="ALLOCATEDACCOUNT"/>
</many-to-one>
<property name="number" type="string">
<column name="NUMBER" length="25" not-null="true" />
</property>
<property name="allocated" type="boolean">
<column name="ALLOCATED" not-null="true" />
</property>
<property name="alias" type="string">
<column name="ALIAS" length="25" />
</property>
<property name="geographic" type="java.lang.Boolean">
<column name="GEOGRAPHIC" />
</property>
</class>
</hibernate-mapping>
And my entity class
还有我的实体课
package entity;
// Generated 21 Feb 2016 1:44:55 AM by Hibernate Tools 3.2.1.GA
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* Numbers generated by hbm2java
*/
@Entity
@Table(name = "NUMBERS", schema = "PUBLIC", catalog = "PUBLIC")
public class Numbers implements java.io.Serializable {
private Integer pk;
private String number;
private Account account;
private boolean allocated;
private String alias;
private Boolean geographic;
public Numbers() {
}
public Numbers(Integer pk, String number, boolean allocated) {
this.pk = pk;
this.number = number;
this.allocated = allocated;
}
public Numbers(Integer pk, Account account, String number, boolean allocated, String alias, Boolean geographic) {
this.pk = pk;
this.account = account;
this.number = number;
this.allocated = allocated;
this.alias = alias;
this.geographic = geographic;
}
@Id
@Column(name = "PK", unique = true, nullable = false)
public Integer getPk() {
return this.pk;
}
public void setPk(Integer pk) {
this.pk = pk;
}
@Column(name = "NUMBER", nullable = false, length = 25)
public String getNumber() {
return this.number;
}
public void setNumber(String number) {
this.number = number;
}
@Column(name = "ALLOCATED", nullable = false)
public boolean isAllocated() {
return this.allocated;
}
public void setAllocated(boolean allocated) {
this.allocated = allocated;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ALLOCATEDACCOUNT")
public Account getAccount() {
return this.account;
}
public void setAccount(Account account) {
this.account = account;
}
@Column(name = "ALIAS", length = 25)
public String getAlias() {
return this.alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
@Column(name = "GEOGRAPHIC")
public Boolean getGeographic() {
return this.geographic;
}
public void setGeographic(Boolean geographic) {
this.geographic = geographic;
}
}
and lastly the code that I am trying to execute
最后是我试图执行的代码
public boolean saveNumbers(Set numbersSet) {
try {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
for (Iterator<Numbers> it = numbersSet.iterator(); it.hasNext();) {
Numbers numbers = it.next();
if (numbers.getPk() == null) {
numbers.setPk(new Integer(5));
session.save(numbers);
} else {
numbers = (Numbers) session.merge(numbers);
session.saveOrUpdate(numbers);
}
}
session.getTransaction().commit();
return true;
} catch (HibernateException he) {
he.printStackTrace();
return false;
}
}
The exception is getting thrown by session.save(numbers);
session.save(数字)抛出异常;
Even when I set the primary key manually, as seen in the above sample it still throwing an exception. I have tried to set the annotations as seen below but this still gives me the same exception.
即使我手动设置主键,如上面的示例所示,它仍然会抛出异常。我试图设置如下所示的注释,但这仍然给我相同的例外。
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "PK", unique = true, nullable = false)
your assistance would be greatly appreciated
非常感谢您的协助
Thank you
谢谢
2 个解决方案
#1
3
Good Morning All
大家早上好
So after much pain and suffering the answer to the issue was as follows, when creating your tables don't forget to set the primary key to IDENTITY. Thank you everyone for your thoughts.
因此,经过多次痛苦和痛苦后,问题的答案如下,在创建表时不要忘记将主键设置为IDENTITY。谢谢大家的意见。
Regards
问候
Brian
布赖恩
#2
0
You're assigning the same PK 5 to all entities without already assigned PK. That means, that you try to save all new instances with exactly the same PK value.
您正在为没有分配PK的所有实体分配相同的PK 5。这意味着,您尝试使用完全相同的PK值保存所有新实例。
If you're using generated ID, don't assign anything to your PK field of the entity.
如果您正在使用生成的ID,请不要为实体的PK字段分配任何内容。
#1
3
Good Morning All
大家早上好
So after much pain and suffering the answer to the issue was as follows, when creating your tables don't forget to set the primary key to IDENTITY. Thank you everyone for your thoughts.
因此,经过多次痛苦和痛苦后,问题的答案如下,在创建表时不要忘记将主键设置为IDENTITY。谢谢大家的意见。
Regards
问候
Brian
布赖恩
#2
0
You're assigning the same PK 5 to all entities without already assigned PK. That means, that you try to save all new instances with exactly the same PK value.
您正在为没有分配PK的所有实体分配相同的PK 5。这意味着,您尝试使用完全相同的PK值保存所有新实例。
If you're using generated ID, don't assign anything to your PK field of the entity.
如果您正在使用生成的ID,请不要为实体的PK字段分配任何内容。