0

当我尝试保存方法时,它给了我一个错误,但是当我尝试手动将数据直接插入数据库时​​,它工作正常。请问在我的 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)
4

1 回答 1

0

正如异常日志明确指出的那样,您有一个ConstraintViolationException. 在您的情况下,异常是由注释引起的,该注释由您的实现中的方法@NotNull生成。PrePersistJPA

删除和字段的@NotNull注释会有所帮助,但您可能会从数据库中获得另一个异常。只是给你一个想法,如果你的 IDE 已经自动生成了注解,这可能是由于 db 表中的字段定义。如果这是您的情况,并且您正在使用像我这样的 MySql 数据库,您将获得:stateIdstateName@NotNull

Internal Exception: java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Column 'state_id' cannot be null"

在这种情况下,您可能希望在 bean 中为您的 State提供 anstateId和 a (并避免删除注释):stateName@NotNull

state.setStateId(1);
state.setStateName("MyState");

最后,如果您也将 设置country_id为非可选字段,如注释所示,您还必须在持久化之前将国家/地区分配给您的州。

于 2012-08-02T19:41:29.183 回答