我正在尝试创建填充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
}
我通过从请求中检索值并将其设置为admin对象来创建管理对象,类似地创建管理员健康计划集对象并在admin对象中设置它,最后发送对象以保存到db。
它给出了如下例外
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);
坚持使用db。
任何人都可以告诉我我错过了什么吗?
答案 0 :(得分:2)
我使用EclipseLink,所以我不确定它在hibernate中是否有所不同,但这是我看错了:
1-您没有将@Column注释添加到Admin实体的id属性。
2 - 我总是使用GenerationType.IDENTITY来表示pks。
3 - 在表AdminHealthPlan中,admin属性具有JoinColumn批注,但您未包含referencedColumnName属性。它应该是这样的:@JoinColumn(name = "admin_id", referencedColumnName = "admin_id")
4-为了向Admin实体添加新的AdminHealthPlan,我建议您执行以下操作:在Admin类中创建一个名为“addAdminHealthPlan”的方法:
public void addAdminHealthPlan(AdminHealthPlan ahp) {
if(healthPlans == null) {
healthPlans = new HashSet<AdminHealthPlan>();
}
ahp.setAdmin(this);
healthPlans.add(ahp);
}
我不知道getJpaTemplate()方法是什么,对象“t”是什么,但这是坚持管理员的逻辑:
Admin admin = createAdminDummyObject();
AdminHealthPlan ahp = new AdminHealthPlan();
ahp.setAdmin(admin);
ahp.setHealthPlan("Plan1");
admin.addAdminHealthPlan(ahp);
entityManager.persist(admin);