我遇到了JPA ManyToOne关系的问题。我有两个实体(HikeList,User):
@Entity
@Table(name = "T_HIKELIST")
public class Hikelist implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private long id;
@Size(min = 1, max = 50)
@ManyToOne(optional = false)
@JoinColumn(name = "login", referencedColumnName = "login")
private User user;
...
}
并且
@Entity
@Table(name = "T_USER")
public class User implements Serializable {
@NotNull
@Size(min = 0, max = 50)
@Id
private String login;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private Collection<Hikelist> hikeListCollection;
...
}
我的测试班:
...
@Test
public void testCreateUser() {
User user = new User();
user.setLogin("thomaslogin");
user.setEmail("thomas@gmail.com");
user.setPassword("thomas");
user.setFirstName("thomas");
user.setLastName("thomas");
userService.createUser(user);
Hikelist hike = new Hikelist();
hike.setId(new Long(1L));
hike.setLabel("GR20");
hike.setUser(user);
hikeService.createHikeList(hike);
Hikelist hike1 = hikeRepository.findOne(new Long(1L));
assertNotNull(hike1);
}
我打电话时遇到以下错误:hikeService.createHikeList(hike);
有人知道我哪里错吗? 提前谢谢。
org.springframework.transaction.TransactionSystemException:无法 提交JPA事务;嵌套异常是 javax.persistence.RollbackException:提交时出错 交易在 org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:524) 在 org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757) 在 org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726) 在 org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:478) 在 org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:272) 在 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 在 org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644) 在 com.breizh.packlight.service.HikelistService $$ EnhancerBySpringCGLIB $$ 38379214.createHikeList() 在 com.breizh.packlight.service.HikelistServiceTest.testCreateUser(HikelistServiceTest.java:60) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:606)at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 在 org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 在 org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) 在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 at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)at at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)at at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 在 org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 在org.junit.runners.ParentRunner.run(ParentRunner.java:309)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 引起:javax.persistence.RollbackException:提交时出错 交易在 org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:94) 在 org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:515) ... 37更多引起:javax.validation.UnexpectedTypeException: HV000030:找不到类型的验证器: com.breizh.packlight.domain.User。在 org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.verifyResolveWasUnique(ConstraintValidatorManager.java:218) 在 org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.findMatchingValidatorClass(ConstraintValidatorManager.java:193) 在 org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager.getInitializedValidator(ConstraintValidatorManager.java:97) 在 org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:125) 在 org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:91) 在 org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:85) 在 org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:478) 在 org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:424) 在 org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:388) 在 org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:340) 在 org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:158) 在 org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:137) 在 org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:95) 在 org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:206) 在 org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:96) 在 org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:453) 在 org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345) 在 org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) 在 org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) 在org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218) 在 org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:421) 在 org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) 在 org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177) 在 org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77) ......还有38个
答案 0 :(得分:1)
错误是:
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: com.breizh.packlight.domain.User
由
引起@Size(min = 1, max = 50)
private User user;
用户的大小如何在1到50之间?
验证约束适用于Java对象。不是数据库中的列。您已确保登录的大小正确,并且用户字段已映射到具有外键约束的列,因此此处@Size
注释不仅不起作用,而且不必要。