Java Hibernate有很多问题

时间:2011-08-18 06:09:22

标签: java hibernate

我的Hibernate Mapping有问题。 我的实体之间有多对一的映射。 我的Oracle DB中的两个表是以下...

Employee                                                                                                                                                      
--------------------------
EMPLOYEE_ID  
EMPLOYEE_FIRST_NAME   
EMPLOYEE_LAST_NAME   
HEALTH_PLAN_ID                                                                                                                                                   
Health_Plan
------------------=
HEALTH_PLAN_ID
HEALTH_PLAN_NAME
HEALTH_PLAN_RATE

我的映射...

@Entity
@Table(name = "Employee")
@SequenceGenerator(name = "employee_seq", sequenceName = "employee_seq")
public class Employee {
    private int employeeId;
    private String firstName;
    private String lastName;
    private HealthPlan plan;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "employee_seq")
    @Column(name = "employee_id", nullable = false)
    public int getEmployeeId() {
        return employeeId;
    }
    @Column(name = "employee_first_name", nullable = false)
    public String getFirstName() {
        return firstName;
    }
    @Column(name = "employee_last_name", nullable = false)
    public String getLastName() {
        return lastName;
    }
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "HEALTH_PLAN_ID")
    public HealthPlan getPlan() {
        return plan;
    }
}

@Entity
@Table(name = "HEALTH_PLAN")
@SequenceGenerator(name = "HEALTH_PLAN_SEQ", sequenceName = "HEALTH_PLAN_SEQ")
public class HealthPlan {
    private int healthPlanId;
    private String healthPlanName;
    private double healthPlanRate;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HEALTH_PLAN_SEQ")
    @Column(nullable = false, name = "HEALTH_PLAN_ID")
    public int getHealthPlanId() {
        return healthPlanId;
    }
    @Column(nullable = false, name = "HEALTH_PLAN_NAME", unique = true)
    public String getHealthPlanName() {
        return healthPlanName;
    }
    @Column(nullable = false, name = "HEALTH_PLAN_RATE")
    public double getHealthPlanRate() {
        return healthPlanRate;
    }
}

当我跑到代码下面时......

    public static void main(String[] args) throws SQLException {        
        HealthPlanDaoImpl healthDao = new HealthPlanDaoImpl();
        EmployeeDaoImpl empDao = new EmployeeDaoImpl();

        HealthPlan plan = new HealthPlan();
        plan.setHealthPlanName("PLAN B");
        plan.setHealthPlanRate(5.0);

        Employee emp = new Employee();
        emp.setPlan(plan);
        emp.setFirstName("Jane");
        emp.setLastName("Doe");
        boolean isSuccess = empDao.addEmployee(emp);
        System.out.println(isSuccess);

    }
public class EmployeeDaoImpl{
    public  boolean addEmployee(Employee emp) {
      boolean bolReturn = true;

      Session session = HibernateUtil.beginTransaction();
      try {
          session.save(emp);
          HibernateUtil.commitTransaction();
      } catch (Exception e) {
          e.printStackTrace();
          bolReturn = false;
          HibernateUtil.rollbackTransaction();
      } 
      return bolReturn;
    }
}

在Health_Plan表中,HEALTH_PLAN_ID = 3(这是正确的!!!) 在员工表中,HEALTH_PLAN_ID = 1850(这个值来自哪里?我希望这也是3。)

我尝试过几次,我注意到在员工表中,HEALTH_PLAN_ID只增加了300。 我想我已经设置了级联选项。

任何提示?

2 个答案:

答案 0 :(得分:1)

HealthPlan plan = new HealthPlan();
plan.setHealthPlanName("PLAN B");
plan.setHealthPlanRate(5.0);

Employee emp = new Employee();
emp.setPlan(plan);
emp.setFirstName("Jane");
emp.setLastName("Doe");

为每个创建的HealthPlan实例创建一个新的Employee实例。原始HealthPlan实例未在新Employee和现有HealthPlan之间的新映射中使用。如果您查询现有的HealthPlan实例,然后将其设置为每个Employee,您会发现健康计划ID在各个对象之间是一致的。

HEALTH_PLAN_ID值的差异可归因于上述测试代码的行为。此外,序列分配大小将增加50,这是默认值,并且您恰好看到了六个连续序列递增操作的结果。请注意,Hibernate将更新不同事务中的序列,因此即使您回滚当前事务,您也会发现序列值将递增。

另一方面,我建议不要使用单向@ManyToOne关系,其值为CascadeType ALL。如果删除了一个HealthPlan,您是否真的要删除所有Employee个实例的Employee,或者就此而言,允许来自Employee的HealthPlan个实例的更新进行传播合并事件?

答案 1 :(得分:0)

我不知道问题的确切原因,但我可以立即看到代码的一些问题。 1.在健康计划表中,注释表示GenerationType.SEQUENCE,在employee表中,它表​​示GenerationType.AUTO 2.在员工表中@Column(name =“ employee_id ”,nullable = false)。 employee_id既不是实体类中变量的名称,也不是实际列的名称。该实体类中的三列存在此问题。

如果您解决此问题,可能会解决您的问题吗?