【问题标题】:ManyToMany relation with extra column and no composite key concept using Spring and Hibernate使用 Spring 和 Hibernate 与额外列和没有复合键概念的多对多关系
【发布时间】:2013-10-07 07:08:32
【问题描述】:

我有两个表:Trade 和 Location,并且存在多对多关系。现在我必须在中间表 trade_location 中添加额外的列作为“status”。我搜索了很多,但提供的所有解决方案都是复合的关键。但我想要一个没有复合键的解决方案作为“trade_location”表作为它自己的唯一主键作为“id”。任何建议使用额外的列和没有复合键来实现多对多关系。 谢谢。这是我的pojo:

    enter code here
          @Entity
          @Table(name="trade")
         public class Trade {
     @Id
     @GeneratedValue
     private Long id;
         @Column(unique=true)
     private String name;

     @OneToOne
     @JoinColumn(name="sectorId")
     private Sector sector;

    @ManyToMany(mappedBy = "trades",fetch=FetchType.EAGER)
    private Set<Location> location =new HashSet<Location>();
    public Long getId() {
     return id;
    }

   @ManyToMany(mappedBy = "trades")
   private Set<Trainee> trainee = new HashSet<Trainee>();
       //getter and setter here
    }

    @Entity
    @Table(name="location")
    public class Location implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private Long id;


    private String name;
    private Long stateId;
    private Long districtId;
    private Long cityId;
    private Long zoneId;
    @Column(name="yearEstablishment")
    private String yearOfEstablishment;

    @ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name = "trade_location",
            joinColumns={@JoinColumn(name="locationId")},
            inverseJoinColumns={@JoinColumn(name="tradeId")})
    private Set<Trade> trades = new HashSet<Trade>();
        //getter and setter
 }

//修改了一个

@Entity
@Table(name="trade")
public class Trade {

@Id
@GeneratedValue
private Long id;
@Column(unique=true)
private String name;

@OneToOne
@JoinColumn(name="sectorId")
private Sector sector;

/*@ManyToMany(mappedBy = "trades",fetch=FetchType.EAGER)
private Set<Location> location =new HashSet<Location>();*/

@OneToMany(mappedBy="trade")
private Set<TradeLocation> tradeLocation = new HashSet<TradeLocation>(0);

@ManyToMany(mappedBy = "trades")
private Set<Trainee> trainee = new HashSet<Trainee>();

@OneToMany
@JoinColumn(name="tradeId")
private Set<Batch>batches =new HashSet<Batch>();
//getter and setter

}

@Entity

@Table(name="位置") 公共类位置 {

@Id
@GeneratedValue
private Long id;


private String name;
private Long stateId;
private Long districtId;
private Long cityId;
private Long zoneId;
@Column(name="yearEstablishment")
private String yearOfEstablishment;

/*@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name = "trade_location",
            joinColumns={@JoinColumn(name="locationId")},
            inverseJoinColumns={@JoinColumn(name="tradeId")})
private Set<Trade> trades = new HashSet<Trade>();*/

@OneToMany(mappedBy="location")
private Set<TradeLocation>tradeLocation=new HashSet<TradeLocation>(0);

/*getter and setter*/

}

@Entity
@Table(name = "trade_location")
public class TradeLocation implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private Long id;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="tradeId")
private Trade trade;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="locationId")
private Location location;

//getter and setter

}

【问题讨论】:

    标签: spring hibernate


    【解决方案1】:

    如果您希望它充当具有 id 的独立表,则必须创建一个名为 TradeLocation 的实体。

    并将您的关系更改为关注。

    Trade 1..* TradeLocation *..1 Location

    【讨论】:

    • 我做了同样的事情,但它不起作用,因为 Trade 也与 Batch 类有一对一的关系。它给出以下错误:执行加载命令时出错:org.hibernate.HibernateException:超过一行找到具有给定标识符的:5
    猜你喜欢
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多