gpt4 book ai didi

java - 更新合并 onetomany jpa/hibernate - ConstraintViolationImpl

转载 作者:行者123 更新时间:2023-12-01 23:50:15 24 4
gpt4 key购买 nike

我正在尝试对 jpa 中的列表进行简单更新,但它不起作用。我有类似的东西。

@Entity
@Table(name = "user")
@DynamicUpdate
public class User implements Serializable {

@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.LAZY)
private List<UserLangs> userLangsList;

}

和表格

@Entity
@Table(name = "user_langs")
public class UserLangs implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(generator = "foreign")
@GenericGenerator(
name = "foreign",
strategy = "foreign",
parameters = {
@org.hibernate.annotations.Parameter(name = "property", value = "user")
})
@Basic(optional = false)
@Column(name = "userid")
private Long userid;

@Basic(optional = false)
@NotNull
@Id
@Column(name = "lang")
private String lang;

@Basic(optional = false)
@NotNull
@Column(name = "level")
private int level;

@JoinColumn(
name = "userid",
referencedColumnName = "id",
insertable = false,
updatable = false)
@ManyToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.LAZY)
private User user;

因此,当我尝试从用户对象更新列表 UserLangs 时,会得到以下异常(exception):

WARNING - Unexpected exception from beforeCompletion; transaction will roll back

javax.persistence.PersistenceException: error during managed flush
at org.hibernate.ejb.AbstractEntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1515)
at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:117)
at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:53)
at org.apache.geronimo.transaction.manager.TransactionImpl.beforeCompletion(TransactionImpl.java:527)
at org.apache.geronimo.transaction.manager.TransactionImpl.beforeCompletion(TransactionImpl.java:511)
at org.apache.geronimo.transaction.manager.TransactionImpl.beforePrepare(TransactionImpl.java:413)
at org.apache.geronimo.transaction.manager.TransactionImpl.commit(TransactionImpl.java:262)
at org.apache.geronimo.transaction.manager.TransactionManagerImpl.commit(TransactionManagerImpl.java:252)
at org.apache.openejb.core.transaction.JtaTransactionPolicy.completeTransaction(JtaTransactionPolicy.java:328)
at org.apache.openejb.core.transaction.TxRequired.commit(TxRequired.java:75)
at org.apache.openejb.core.transaction.EjbTransactionUtil.afterInvoke(EjbTransactionUtil.java:73)
at org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:257)
at org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:189)
at org.apache.openejb.core.ivm.EjbObjectProxyHandler.synchronizedBusinessMethod(EjbObjectProxyHandler.java:253)
at org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(EjbObjectProxyHandler.java:248)
at org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(EjbObjectProxyHandler.java:92)
at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:279)
at com.polyglong.dao.UsersDAO$LocalBeanProxy.updateUser(com/polyglong/dao/UsersDAO.java)
at com.polyglong.dao.UserDAOTest.getUpdateUser(UserDAOTest.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:45)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [com.polyglong.entities.user.UserLangs] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{rootBean=com.polyglong.entities.UserLangs[ userid=4,lang=null,level=0 ], propertyPath='lang', message='may not be null', leafBean=com.polyglong.entities.UserLangs[ userid=4,lang=null,level=0 ], value=null}
]
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventLi stener.java:159)
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94)
at org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:181)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:328)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1233)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:112)
... 47 more

根据执行,lang试图被设置为null,但这是奇怪的部分,因为所有内容都设置正确并且对象中没有null,但由于某种原因在提交后它会回滚。

当尝试仅更新对象用户时,合并工作正常,但是当我尝试更新其中的 userlangs 列表时,它失败了。

谢谢

最佳答案

您将 lang 属性标记为 @NotNull,但您试图保存一个 null 属性。您收到的错误是这样的约束违规。

关于java - 更新合并 onetomany jpa/hibernate - ConstraintViolationImpl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16399086/

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