我有IP类。其中一个类参数是User class。
我想在用户和ip之间建立关系。 我想添加与用户相关的实体IP,但表单中不需要用户实体。所以我应该能够在没有用户相关的情况下添加IP实体(在用户外键列的ip表中的db应为null)。
我如何在hiberanate / jpa
中实现这一点我的Ip班:
@Entity
public class Ip {
/** The id. */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@Column(length = 50)
@Constraints.Required
public String address;
@ManyToOne(optional = true,cascade = CascadeType.ALL)
public User user;
// other code //
}
和用户类:
@Entity
@Table(name="common__User")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long id;
@Constraints.Required
public String email;
@Constraints.Required
public String password;
@ManyToOne(cascade = CascadeType.PERSIST)
@Constraints.Required
public Customer customer;
@ManyToOne(cascade=CascadeType.MERGE)
public Department department;
@ManyToMany(cascade = CascadeType.PERSIST)
public List<Role> role;
@Constraints.Required
public boolean system;
@Constraints.Required
public boolean active;
}
我的持久化IP实体的方法
public void toDataBase() {
JPA.em().persist(this);
}
我收到错误:
play.api.Application$$anon$1: Execution exception[[ConstraintViolationException: Validation failed for classes [models.User] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='error.required', propertyPath=customer, rootBeanClass=class models.User, messageTemplate='error.required'}
ConstraintViolationImpl{interpolatedMessage='error.required', propertyPath=email, rootBeanClass=class models.User, messageTemplate='error.required'}
ConstraintViolationImpl{interpolatedMessage='error.required', propertyPath=password, rootBeanClass=class models.User, messageTemplate='error.required'}
]]]
at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.4]
at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.4]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10.jar:2.2.4]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10.jar:2.2.4]
at scala.Option.map(Option.scala:145) [scala-library.jar:na]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3.applyOrElse(PlayDefaultUpstreamHandler.scala:264) [play_2.10.jar:2.2.4]
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [models.User] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='error.required', propertyPath=customer, rootBeanClass=class models.User, messageTemplate='error.required'}
ConstraintViolationImpl{interpolatedMessage='error.required', propertyPath=email, rootBeanClass=class models.User, messageTemplate='error.required'}
ConstraintViolationImpl{interpolatedMessage='error.required', propertyPath=password, rootBeanClass=class models.User, messageTemplate='error.required'}
]
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:161) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.action.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:160) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:65) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
和mysql db结构
# Name Type Collation Attributes Null Default
1 id bigint(20) No None AUTO_INCREMENT
2 address varchar(50) utf8_general_ci Yes NULL
3 user_id bigint(20) Yes NULL
请帮忙