gpt4 book ai didi

java - ConstraintViolationException : NOT NULL when using Spring, HSQL 和 Hibernate

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:15:32 24 4
gpt4 key购买 nike

当我试图在数据库中插入一个 Individual 类型的对象时,我得到了一个 NOT NULL 违反约束的异常。我使用 hsql 版本 2.3.2。我让 hibernate 为我生成数据库表。在正常代码中(我使用 SQLServer 数据库)一切正常。

这是我的Individual Object,ID是数据库生成的,是表的主键。

单个对象:

@XmlRootElement
@Entity
@Table(name="INDIVIDUALS")
public class Individual {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="INDIVIDUAL_ID")
private long id;

在 HealthCareProfessional 对象中,有指向此 Individual 对象的主键的链接:

@Entity
@Table(name="HEALTHCARE_PROFESSIONALS")
public class HealthCareProfessional {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="HEALTHCARE_PROFESSIONAL_ID")
private long id;

@ManyToOne
@JoinColumn(name = "INDIVIDUAL_ID", nullable = false, updatable = false, insertable = true)
private Individual individual;

...

我定义 entityManagerFactory 的应用程序上下文如下所示:

 <jdbc:embedded-database id="dataSource" type="HSQL" />

<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="/persistence-unittest.xml" />
<property name="persistenceUnitName" value="testingSetup"/>
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- Create the database, please -->
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>

<bean id="jpaVendorAdaptor"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>

我的持久化xml定义如下:

 <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">

<persistence-unit name="testingSetup">
<!-- The autodetection only works if the classes are packaged as a JAR. Doesn't work for unit tests. Go figure -->
<class>be.healthconnect.pwg.domain.individual.HealthCareProfessional</class>
<class>be.healthconnect.pwg.domain.individual.Individual</class>

<properties>
<property name="hibernate.archive.autodetection" value="" />
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>

这是我的单元测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = {"classpath:/applicationContext-test.xml"})
@Transactional
public class IndividualDaoTest {

private static final String SSIN_JELLE= "81062205558";
private static final String NIHII = "123";

@Autowired
private IIndividualDao individualDao;

@Before
public void setUp() throws Exception {
Individual individual = buildIndividual(SSIN_JELLE);
individualDao.addIndividual(individual);
}

@Test
public void testGetIndividualsBySSIN() throws Exception {

final List<Individual> individuals = individualDao.getIndividuals(SSIN_JELLE);
Assert.assertNotNull(individuals);
Assert.assertEquals(1, individuals.size());
Individual individual = individuals.get(0);
Assert.assertNotNull(individual);
}

这是我收到的错误信息:

javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10143 table: HEALTHCARE_PROFESSIONALS column: INDIVIDUAL_ID
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1360)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1288)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1294)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:860)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
at com.sun.proxy.$Proxy34.persist(Unknown Source)
at be.healthconnect.pwg.data_access.dao.impl.IndividualDao.addIndividual(IndividualDao.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
at com.sun.proxy.$Proxy36.addIndividual(Unknown Source)
at be.healthconnect.pwg.data_access.dao.impl.IndividualDaoTest.setUp(IndividualDaoTest.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我真的不明白为什么hsql不使用ID的生成值...

最佳答案

删除行 @JoinColumn(name = "INDIVIDUAL_ID", nullable = false, updatable = false, insertable = true) 上的 nullable = false。因为此时 hibernate 正在制作链接 ManyToOne 个人的 ID 尚未建立,从而导致您的错误。如果你真的想要额外的保护免受 null 值的影响,那么在 @JoinColumn 之外使用 @NotNull@JoinColumn 中的 nullable = false 创建对数据库的约束,而 @NotNull 将其保留在对象层。

关于java - ConstraintViolationException : NOT NULL when using Spring, HSQL 和 Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23760687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com