我正在尝试创建填充 Admin 和 AdminHealthPlan 表。
两者之间的关系是一对多的(一个管理员可以拥有多个健康计划)。
该类Admin
定义如下:
@Entity
@Table(name = "admin")
@NamedQueries(value = {
@NamedQuery(name = "admin.getAdminById", query = "SELECT a "
+ "from Admin a WHERE a.id=:id and deleted = 0"),
@NamedQuery(name = "admin.getAdminByEmail", query = "SELECT a "
+ "from Admin a where a.email = :email and deleted = 0") })
public class Admin extends Persistent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "password")
private String password;
@Column(name = "email")
private String email;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "phone")
private String phone;
@OneToOne(mappedBy = "admin", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private AdminRole role;
@OneToOne(mappedBy = "admin", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private AdminPasswordToken passwordToken;
@OneToMany(mappedBy = "admin", fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
private Set<AdminHealthPlan> healthPlans;
@Column(name = "deleted")
private Boolean deleted = false;
@Column(name = "security_question")
private Integer securityQuestion;
@Column(name = "security_answer")
private String securityAnswer;
public Admin() {}
// Getters/setters
}
AdminHealthPlan
类定义如下。
@Entity
@Table(name = "admin_health_plan")
public class AdminHealthPlan implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "health_plan")
private String healthPlan;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "admin_id", nullable = false)
private Admin admin;
@Column(name = "deleted")
private Boolean deleted = false;
public AdminHealthPlan() {}
public AdminHealthPlan(String healthPlan) {
this.healthPlan = healthPlan;
}
// Getters/setters
}
我通过从请求中检索值并将其设置为管理对象来创建管理对象,类似地用于创建管理健康计划集对象并将其设置在管理对象中,最后将对象发送到数据库。
它给出了如下例外
May 21, 2012 6:49:16 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet appServlet threw exception
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'admin_id' cannot be null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy70.executeUpdate(Unknown Source)
ws
创建 Admin 和 AdminHealthPlan 对象:
private Admin createAdminDummyObject() {
Admin admin = new Admin();
admin.setFirstName("Zuned");
admin.setLastName("Ahmed");
admin.setEmail("zuned@hcentive.com");
admin.setPhone("123-1234454");
admin.setHealthPlans(getHealthPlans(admin));
return admin;
}
private Set<AdminHealthPlan> getHealthPlans(Admin admin) {
Set<AdminHealthPlan> adminAHP = new HashSet<AdminHealthPlan>();
AdminHealthPlan ahp = new AdminHealthPlan();
ahp.setAdmin(admin);
ahp.setHealthPlan("Plan1");
adminAHP.add(ahp);
return adminAHP;
}
我用了
getJpaTemplate().persist(t);
坚持在分贝。
任何人都可以告诉我我错过了什么吗?