PopulateAdminテーブルとAdminHealthPlanテーブルを作成しようとしています。
2つの関係は1対多です(1人の管理者が多くの健康保険を所有できます)。
クラスは次の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
}
リクエストから値を取得して管理オブジェクトに設定することで管理オブジェクトを作成します。同様に、管理ヘルスプランの設定オブジェクトを作成して管理オブジェクトに設定し、最後にオブジェクトを送信して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に永続化します。
誰かが私に何が欠けているのか教えてもらえますか?