【发布时间】:2014-10-17 14:08:01
【问题描述】:
我正在尝试保留与 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;
地址实体:
@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)
【问题讨论】:
-
为什么Adresse.id上的@generatedValue被注释掉了?
-
"请注意 MySQL DB 为 School 和 Adresse 生成 id" 我会说这显然不是这种情况......您是否成功地使用了未注释掉的 @GeneratedValue?
-
@generatedValue 被评论是因为我得到了这个异常:内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'mysql.sequence'不存在错误代码:1146
-
它要求一个用于自动生成的序列表!!!!??
-
GenerationType.AUTO 表示你有序列表,所以需要SchemaGeneration 来创建表。