1

我正在尝试创建填充 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);

坚持在分贝。

任何人都可以告诉我我错过了什么吗?

4

1 回答 1

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);
于 2012-05-23T14:10:15.277 回答