Hibernate中的复合键

时间:2013-03-01 08:51:15

标签: hibernate

我想实现一个表格如下

   CREATE TABLE `item` (
  `country_id` bigint(20) NOT NULL,
  `name` varchar(255) NOT NULL,
  `CREATION_DATE` datetime NOT NULL,
  `description` varchar(255) DEFAULT NULL,
  `point` decimal(19,2) NOT NULL,
  `price` decimal(19,2) NOT NULL,
  `UPDATED_DATE` datetime DEFAULT NULL,
  `VERSION` int(11) DEFAULT NULL,
  `STARTER_PACKAGE` tinyint(1) NOT NULL,
  PRIMARY KEY (`name`,`country_id`),
  KEY `fk_country_id` (`country_id`),
  CONSTRAINT `fk_country_id` FOREIGN KEY (`country_id`) REFERENCES `country` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$

有人能告诉我如何使用注释编写实体来实现上表吗?

1 个答案:

答案 0 :(得分:1)

Hibernate有一种处理这种情况的非常优雅的方式:

@Entity
@Table(name = "itemPrice")
public class ItemPrice extends PersistentObject {

   @Id
   @ManyToOne
   @JoinColumn(name = "country_id")
   private Country country;

   @Id
   @ManyToOne
   @JoinColumn(name = "item_id")
   private Item item;

   @Column(nullable = false)
   @NotNull
   private BigDecimal point;

   @Column(nullable = false)
   @NotNull
   private BigDecimal price;
}