用单独的ID连接表

时间:2018-06-25 19:48:44

标签: hibernate jpa spring-data-jpa one-to-many jointable

在我的Spring Boot应用程序中,我具有以下示例架构,该架构包含两个表和一个具有IdClass的联接表:

Company.java

@Entity
@Table(name = "COMPANY")
public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany
    @LazyCollection(LazyCollectionOption.FALSE)
    @JoinTable(name = "COMPANY_2_LOCATIONS", joinColumns = {@JoinColumn(name = "COMPANY_ID", referencedColumnName = "ID")},
        inverseJoinColumns = {@JoinColumn(name = "LOCATION_ID", referencedColumnName = "ID")})
    private List<Location> locations;
    ...

}

Location.java

@Entity
@Table(name = "LOCATION")
public class Location {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    ...

}

Company2Locations.java

@Entity
@IdClass(Company2LocationsId.class)
@Table(name = "COMPANY_2_LOCATIONS")
public class Comany2Locations {

    @Id
    @ManyToOne
    @JoinColumn(name = "COMPANY_ID")
    @NotNull
    private Company company;

    @Id
    @ManyToOne
    @JoinColumn(name = "LOCATION_ID")
    @NotNull
    private Location location;
    ...
}

Company2LocationsId.java

public class Company2LocationsId{

    private Company company;
    private Location location;
    ...
}

我想为连接表“ Company2Locations”添加一个单独的ID,因此该表具有三列:

  • ID //单独的ID
  • COMPANY_ID
  • LOCATION_ID

我已经尝试了以下方法来实现这一目标:

  1. 添加了一个ID并保留@IdClass:

Company2Locations.java

@Entity
@IdClass(Company2LocationsId.class)
@Table(name = "COMPANY_2_LOCATIONS")
public class Comany2Locations {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Id
    @ManyToOne
    @JoinColumn(name = "COMPANY_ID")
    @NotNull
    private Company company;

    @Id
    @ManyToOne
    @JoinColumn(name = "LOCATION_ID")
    @NotNull
    private Location location;
    ...
}

这在我启动应用程序时提供了NullpointerException。

Caused by: java.lang.NullPointerException: null
at org.hibernate.internal.util.StringHelper.root(StringHelper.java:374)
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1832)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1775)
at org.hibernate.hql.internal.ast.tree.FromElement.getIdentityColumns(FromElement.java:352)
  1. 添加了一个ID,并删除了位置和公司上的@IdClass和@Id注释:

Company2Locations.java

@Entity
@Table(name = "COMPANY_2_LOCATIONS")
public class Comany2Locations {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "COMPANY_ID")
    @NotNull
    private Company company;

    @ManyToOne
    @JoinColumn(name = "LOCATION_ID")
    @NotNull
    private Location location;
    ...
}

现在应用程序启动,我可以向公司添加地点。但是不会生成ID,并且ID列包含空值。

有什么想法如何向联接表添加单独的ID?

1 个答案:

答案 0 :(得分:0)

我猜这是克里斯发表评论的正确答案:

您必须在模型中将Comany2Locations(sp)用作Company和Location之间的对象,而不是直接使用公司引用Location。在Company内,它应该是一个使用了对bybyby =“ company”

的OneToMany