Hibernate创建Composite Key而不是使用Identifier

时间:2014-07-25 13:54:12

标签: java hibernate orm hibernate-mapping composite-key

我有一个Entity FooBar,它作为Foo和Bar实体的@ManyToMany连接表,包括一些附加信息。

@Entity
@Table(name = "foo_bar")
public class FooBar
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
    protected Long id;

    @Column(name = "someInfo", nullable = true)
    private String someInfo;

    @ManyToOne(optional = false)
    private Foo foo;

    @ManyToOne(optional = false)
    private Bar bar;


    //getters, setters, and toString()
}

@Entity
@Table(name = "foo")
public class Foo
{
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id", unique = true, nullable = false)
   protected Long id;

   @OneToMany(mappedBy = "foo", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   private Set<FooBar> fooBars;

   //Foo has a number of other fields
   @Column(name = "orderIndex", nullable = false)
   private int orderIndex;

   @Column(name = "upgradeDirection", nullable = false)
   @Enumerated(EnumType.STRING)
   private Order direction;

   @ManyToOne(optional = false)
   private SomeEntity e;


   //getters, setters, and toString()
}


@Entity
@Table(name = "bar")
public class Bar
{
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id", unique = true, nullable = false)
   protected Long id;

   @OneToMany(mappedBy = "bar") //TODO specify a cascade and fetch attribute
   private Set<FooBar> fooBars;

   //Bar contains a number of other fields
   @Column(name = "value", nullable = false)
   private String value;


   //getters, setters, and toString()
}

当Hibernate创建表时,其中包含列&#39; id&#39;,&#39; someInfo&#39;,&#39; foo_id&#39;和&#39; bar_id&#39;。 &#39; foo_id&#39;和&#39; bar_id&#39;用作复合键而不是使用&#39; id&#39;领域,任何想法为什么?

0 个答案:

没有答案