JPA:专栏' AdressId'不能为空

时间:2014-10-17 14:08:01

标签: java mysql jpa

我正在尝试坚持与Adresse有单向oneToOne关系的学校实体,但我得到Column 'AdressId' cannot be null,请注意MySQL DB为School和Adresse生成ID是我的代码: 在JSF中,当单击提交按钮时,将调用createSchool方法。

@Named
@RequestScoped
public class SchoolAdd extends BaseBacking implements Serializable{
private static final long serialVersionUID = 1L;
private static final String INSCRIPTION_RETURN = "/login.xhtml?faces-redirect=true";
private static final String RB_Name = "Bundle.messages";
private static final String USER_NOT_FOUND = "user.not.found";

private String userEmail;

private User user;
private School school;
private Adresse adress;

@EJB
private SchoolPr schoolPr;

@EJB
private UserPr userPr;


public SchoolAdd() {
    school = new School();
    adress = new Adresse();

    school.setPhoneNumbers(new HashMap<PhoneTypeSchool, String>());
}

public String createSchool() {
    userEmail = getRequest().getUserPrincipal().getName();
    System.out.println(userEmail);
    try {
        user = userPr.getUserByEmail(userEmail);
    } catch (Exception e1) {
        getContext().addMessage(null, new FacesMessage(ResourceBundleLoader.getBundle(RB_Name, USER_NOT_FOUND)));//DEL DEBUG
    }
    school.setUser(user);
    school.setAdresse(adress);
    System.out.println("1: " + adress.getCountry());
    System.out.println("2: " + school.getAdresse().getCountry());
    schoolPr.createSchool(school);

    return INSCRIPTION_RETURN;
}+getters & stters for school & adress

学校实体

@Entity
@Table(schema = "school", name = "school")
public class School implements Serializable {

private static final long serialVersionUID = 1L;

@Id
private BigInteger id;

private String name;

@ManyToOne(fetch=FetchType.EAGER, optional=false)
@JoinColumn(name = "userId")
private User user;

@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL, optional = false)
@JoinColumn(name = "adressId", referencedColumnName = "ID")
private Adresse adresse;

@Temporal(TemporalType.DATE)
private Date creationDate;

@ElementCollection
@CollectionTable(name="SCHOOL_PHONE")
@MapKeyEnumerated(EnumType.STRING)
@MapKeyColumn(name="PHONE_TYPE")
@Column(name="PHONE_NUM")
private Map<PhoneTypeSchool, String> phoneNumbers;

@Column(name = "JOIN_DATE")
private Timestamp joinDate;

Adresse Entity:

@Entity
@Table(schema = "school", name = "adress")
public class Adresse implements java.io.Serializable {
private static final long serialVersionUID = 1L;

@Id
//@GeneratedValue(strategy=GenerationType.AUTO)
private BigInteger id;

private String country;

private String state;

private String city;

private String street;

private String number;

private String zip;

更新: 如果使用@GeneratedValue(strategy = GenerationType.AUTO),我得到:

Avertissement: Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b):         org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mysql.sequence' doesn't exist
Error Code: 1146
Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
bind => [2 parameters bound]
Query: DataModifyQuery(name="SEQUENCE" sql="UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?")
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)

1 个答案:

答案 0 :(得分:2)

我将它从AUTO更改为IDENTITY,现在可以正常工作。