当我尝试保存方法时,它给了我一个错误,但是当我尝试手动将数据直接插入数据库时,它工作正常。请问在我的 managedbean 类中有没有添加或看到的东西?请帮助保持解释尽可能简单和直接。谢谢
@Entity
@Table(name = "state")
@NamedQueries({
@NamedQuery(name = "State.findAll", query = "SELECT s FROM State s"),
@NamedQuery(name = "State.findByStateId", query = "SELECT s FROM State s WHERE s.stateId = :stateId"),
@NamedQuery(name = "State.findByStateName", query = "SELECT s FROM State s WHERE s.stateName = :stateName")})
public class State implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "state_id")
private Integer stateId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "state_name")
private String stateName;
@JoinColumn(name = "country_id", referencedColumnName = "country_id")
@ManyToOne(optional = false)
private Country countryId;
}
这是国家实体
@Entity
@Table(name = "country")
@NamedQueries({
@NamedQuery(name = "Country.findAll", query = "SELECT c FROM Country c"),
@NamedQuery(name = "Country.findByCountryId", query = "SELECT c FROM Country c WHERE c.countryId = :countryId"),
@NamedQuery(name = "Country.findByCountryName", query = "SELECT c FROM Country c WHERE c.countryName = :countryName")})
public class Country implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "country_id")
private Integer countryId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "country_name")
private String countryName;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "countryId")
private List<State> stateList;
}
托管豆
public class stateManagedBean {
@EJB
private SessionBean sessionBean;
@EJB
private stateSessionBean stateSessionBean;
private State state = new State();
public stateManagedBean() {
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public void save(){
stateSessionBean.persist(state);
}
}
错误页面
javax.faces.el.EvaluationException: javax.ejb.EJBException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
Caused by: javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'. Please refer to embedded ConstraintViolations for details.
at org.eclipse.persistence.internal.jpa.metadata.listeners.BeanValidationListener.validateOnCallbackEvent(BeanValidationListener.java:90)
at org.eclipse.persistence.internal.jpa.metadata.listeners.BeanValidationListener.prePersist(BeanValidationListener.java:62)
at org.eclipse.persistence.descriptors.DescriptorEventManager.notifyListener(DescriptorEventManager.java:698)