使用额外的列,代理键,附加主键来连接多个到多个映射的连接表

时间:2011-08-08 19:01:39

标签: hibernate nhibernate nhibernate-mapping hibernate-annotations

我很难为以下场景解决实体映射(使用注释)(来自遗留数据库)

情景1:

Table A
  A_ID (PK)

Table B    
  B_ID (PK)

TABLE A_B    
  AB_ID (PK)    
  A_ID (FK)    
  B_ID (FK)    
  Created_DATE

情景2:

Table A    
  A_ID (PK)

Table B    
  B_ID (PK)

TABLE A_B    
  AB_ID (PK)    
  A_ID (PK)(FK)    
  Test_Date (PK)    
  B_ID (FK)    
  Created_DATE

场景3:

Table A
  A_ID (PK)             
  A2_ID (PK)

Table B    
  B_ID (PK)

TABLE A_B    
  A_ID (PK)(FK)    
  A2_ID (PK)(FK)    
  B_ID (PK)(FK)   
  Created_DATE

有些人可以使用注释和解释来在休眠中显示这些实体吗?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

Table Products
  Product_Id(PK)
  Product_Name
  Price

Table Orders
  Order_Id(PK)
  Order_Date
  Total

Table Order_Products
  Order_Id(FK)(PK)
  Product_Id(FK)(PK)
  Quantity

 @Entity
  public class OrderProducts{

     @Embeddable
     public static class IdClass {

        @ManyToOne
        @JoinColumn(name = "Product_Id")
        private Products p;

        @ManyToOne
        @JoinColumn(name = "Order_Id")
        private Orders o;
   }

   @Id
   private IdClass id = new IdClass();
   private Integer quantity;
  }

  @Entity
  public class Products{
     @ID
     private Integer id;

     @OneToMany(mappedBy="id.p") // this is the tricky part 'id.p'
     Set<OrderProducts> ops = new Set<OrderProducts>();

   }

与Orders类似。