简体   繁体   English

如何使用一对多单向删除两个表之间的关系

[英]How to delete relation between two tables with one-to-many unidirectional

I have two entities bound with one-to-many relationship. 我有两个受一对多关系约束的实体。 But one entity can exist without the other. 但是一个实体可以不存在另一个实体而存在。 So the relationship is uni-directional. 因此,关系是单向的。 As this; 这样

@Entity
public class TransportationOrderProduct  {

    @OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    private List<WarehousePackage> selectedWarehousePackages;
}

@Entity
public class WarehousePackage{
}

And hibernate created these tables; 休眠创建了这些表;

TransportationOrderProduct 运输订单产品

  • id ID

TransportationOrderProductSelectedWarehousePackages 运输订单产品选定的仓库包装

  • transportationOrderProductId transportationOrderProductId
  • selectedWarehousePackageId selectedWarehousePackageId

WarehousePackage 仓库包装

  • id ID

When fetching the collection (selectedWarehousePackages) everything works fine. 在获取集合(selectedWarehousePackages)时,一切正常。 But when I clear the TransportationOrderProduct.selectedWarehousePackages list, and add new ones, Hibernate throws DuplicateEntry exception. 但是当我清除TransportationOrderProduct.selectedWarehousePackages列表并添加新的列表时,Hibernate抛出DuplicateEntry异常。 Saying that transportationOrderProductId in the TransportationOrderProductSelectedWarehousePackages table cannot be inserted twice. 说,不能两次插入TransportationOrderProductSelectedWarehousePackages表中的transportationOrderProductId。

I think this is because Hibernate doesn't delete the relation in the TransportationOrderProductSelectedWarehousePackages table when I call; 我认为这是因为在我调用时,Hibernate不会删除TransportationOrderProductSelectedWarehousePackages表中的关系。

TransportationOrderProduct.selectedWarehousePackages.clear()

And add some entities after ; 并在之后添加一些实体;

TransportationOrderProduct.selectedWarehousePackages.add(newPackage)
TransportationOrderProduct.selectedWarehousePackages.add(newPackage)
.
.

Can somebody help? 有人可以帮忙吗?

Its sounds the relation is one-Many as I understand 听起来这种关系是一对多的,据我所知

在此处输入图片说明

Don't use unidirectional one-to-many associations 不要使用单向一对多关联

avoid unidirectional one-to-many associations in your domain model. 避免域模型中的单向一对多关联。 Otherwise, Hibernate might create unexpected tables and execute more SQL statements than you expected and this certainly described why hibernate create 3 entities while your implementation should be 2 entities with relation one-2-many. 否则,Hibernate可能会创建意外的表并执行比您预期的更多的SQL语句,这肯定说明了为什么hibernate创建3个实体,而您的实现应是2个实体,关系为2个到2个。

The definition of uni-directional sounds not an issue in our case if we use directional it's OKAY it will serve our purpose you just need attribute to map our association 单向的定义听起来对我们来说不是一个问题,如果我们使用定向没事就会为我所用,你只需要attribute to map our association

using annotation 使用注释

@Entity
public class TransportationOrderProduct  {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "id")
   private long id; 

@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    private Set<WarehousePackage> packages= new HashSet<>();

public Set<WarehousePackage> getPackages() {
    return packages;
}

public void setPackages(Set<WarehousePackage> packages) {
    this.packages = packages;
}

public void addPackages (WarehousePackage value) {
this.packages.add(value);
} 

public void clearPackages (WarehousePackage value) {
    this.packages.clear();
} 
    ...
} 

@Entity
public class WarehousePackage{
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "id")
   private long id; 
}

You can save transportation order with association you need 您可以保存运输订单与所需的关联

TransportationOrderProduct transportation = new TransportationOrderProduct  ();

Set<WarehousePackage> packages = new HashSet <> () ; 
WarehousePackage package1 = new WarehousePackage () ;
WarehousePackage package2 = new WarehousePackage () ;
packages.add(package1);
packages.add(package2);
transportation.setPackages(packages) ;

session.save(transportation);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM