【问题标题】:JPA Null parent foreign key in child entity子实体中的 JPA Null 父外键
【发布时间】:2016-12-04 22:41:33
【问题描述】:

我无法将子外键设置为 OneToMany 双向关系中的父级。我有一个这样定义的父类 Union:

@Entity
@PrimaryKeyJoinColumn(referencedColumnName="id")
@Table(name="union_tb")
@Inheritance( strategy = InheritanceType.JOINED )     
public class Union extends User{

@Column(unique=true)
String Name;
float uniondue = 0;


@OneToMany(targetEntity=ServiceCharge.class,cascade=CascadeType.REMOVE, orphanRemoval=true,fetch = FetchType.EAGER,mappedBy="union")
List<ServiceCharge> ServiceCharges;
@OneToMany(targetEntity=Employee.class,cascade=CascadeType.REMOVE, orphanRemoval=true,fetch = FetchType.EAGER)
List<Employee> Employee;

public void PostServiceCharge(ServiceCharge charge) {
    ServiceCharges.add(charge);

}

还有一个子类 ServiceCharge 以这种方式定义:

@Entity
@Table
 public class ServiceCharge implements Serializable{


@Id @GeneratedValue(strategy= GenerationType.AUTO)
int ChargeID;
String Service;
@Temporal(TemporalType.TIMESTAMP)
Calendar  TimeStamp;
float ChargeAmount;
@ManyToOne
@JoinColumn(name="union_id")
Union union;

public ServiceCharge(){super();}




public ServiceCharge(float chargeAmount, String service, String timestamp/*, Union union*/) throws ParseException {
    super();
    ChargeAmount = chargeAmount;
    Service = service;
    TimeStamp = TimeManagUtil.getGCobjectfromString(timestamp);
    //this.Union = union;
}

// getters and setters
}

然后我运行测试代码:

    @Test
public void Test() throws Exception {
    ServiceCharge charge1 = new ServiceCharge(60,"service for employee","27/11/2016 20:35:00");
    Union u = new Union("Sindacate", 80);
    UnionController.add(u);
    ServiceController.add(charge1, u);
}

如您所见,在将 Union u 持久化到数据库后,我将两个实体与控制器关联,ServiceController.add 只是使用 Union setter 将实体 charge1 添加到服务费列表中,然后将实体持久化到数据库。

但是,测试正常,但我只能看到这个:

 | ChargeID | ChargeAmount | Service              | TimeStamp           | union_id |
 +----------+--------------+----------------------+---------------------+----------+
 |        6 |           60 | service for employee | 2016-11-27 20:35:00 |     NULL |
 |        7 |           10 | service for employee | 2016-11-27 20:45:00 |     NULL


 | Name      | uniondue | id |
 +-----------+----------+----+
 | Sindacate |       80 |  5 |
 +-----------+----------+----+

如您所见,union_id 设置为 null。我做错了什么?

【问题讨论】:

    标签: java mysql entity-framework hibernate jpa


    【解决方案1】:

    尝试先实例化Union类,并在ServiceCharge的构造函数中初始化。

    @Test
    public void Test() throws Exception {
        Union u = new Union("Sindacate", 80);
        UnionController.add(u);
        ServiceCharge charge1 = 
                  new ServiceCharge(60,"service for employee","27/11/2016 20:35:00", u); // uncomment your parameter here that accepts Union class type.
        ServiceController.add(charge1, u);
    }
    

    【讨论】:

    • 你救了我。事实上,在概念上它甚至是正确的,因为如果没有与之关联的联合,服务费就无法存在。但是现在,我对 Employee 关系也有同样的问题,但是现在我不应该在构造函数中调用 Union,因为现在它甚至可以为 null,而且我不喜欢每次都将 null 作为参数传递的想法。
    • 那么到目前为止的输出是什么?我认为您可以重载您的构造函数,以便您可以选择应该使用的构造函数。所以你可以有选择。一个有联合参数的构造函数和一个没有联合参数的构造函数。
    • 我不能那样做,因为员工可以在被实例化后决定与工会关联。允许空值。
    • 所以将 ServiceCharge 中的 unionid 更改为空值。将此注释放在您的联盟中。 @Column(nullable = false)
    • 如果是工会和员工之间的问题,那又有什么意义呢?也许我可以在应用关系后尝试合并员工。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 2023-02-10
    • 2022-11-29
    • 2020-01-24
    • 2020-05-16
    • 1970-01-01
    相关资源
    最近更新 更多