Hibernate:如何返回复合ID?

时间:2011-03-01 23:25:52

标签: java hibernate

我有这个复合身份:

<composite-id class="Entities.PackageId" name="id">
  <key-property name="id" type="int">
    <column name="id"/>
  </key-property>
  <key-property name="idProduct" type="int">
    <column name="idProduct"/>
  </key-property>
</composite-id>

实际上我想在包和产品ID中使用自动增量。 但看起来我做不到。

所以,我必须生成一个id来打包,(产品已经有自动增量键),所以我的id是从package.id + 1获取最大值,所以这样我的包有一个新的id

这样做的想法是什么?

祝你好运, Valter Henrique。

-

DDL:

DROP TABLE IF EXISTS `Product`;
CREATE TABLE `Product` (
  `id` int(11) NOT NULL,
  `name` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `package`;
CREATE TABLE `package` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `idProduct` int(11) NOT NULL,
  `quantity` int(11) NOT NULL,
  `mode` char(1) NOT NULL,
  `unity` varchar(25) NOT NULL,
  `description` varchar(150) NOT NULL,
  `email` varchar(50) NOT NULL,
  PRIMARY KEY (`id`,`idProduct`),
  KEY `email` (`email`),
  KEY `idProduct` (`idProduct`),
  CONSTRAINT `package_ibfk_1` FOREIGN KEY (`email`) REFERENCES `usuario` (`email`),
  CONSTRAINT `package_ibfk_2` FOREIGN KEY (`idProduct`) REFERENCES `Product` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `name` varchar(50) NOT NULL,
  `lastname` varchar(100) NOT NULL,
  `gender` varchar(9) NOT NULL,
  `birthday` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `email` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `street` varchar(100) DEFAULT NULL,
  `number` int(11) DEFAULT NULL,
  `complement` varchar(100) DEFAULT NULL,
  `city` varchar(100) DEFAULT NULL,
  `state` varchar(100) DEFAULT NULL,
  `country` varchar(100) DEFAULT NULL,
  `image` varchar(100) DEFAULT NULL,
  `telephone` int(12) DEFAULT NULL,
  `cellphone` int(12) DEFAULT NULL,
  `lat` double DEFAULT NULL,
  `long` double DEFAULT NULL,
  PRIMARY KEY (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

映射.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 25/02/2011 15:56:00 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
  <class catalog="p2p" name="Entities.Package" table="Package">
    <composite-id class="Entities.PackageId" name="id">
      <key-property name="id" type="int">
        <column name="id"/>
      </key-property>
      <key-property name="idProduct" type="int">
        <column name="idProduct"/>
      </key-property>
    </composite-id>
    <many-to-one class="Entities.User" fetch="select" name="User">
      <column length="50" name="email" not-null="true"/>
    </many-to-one>
    <many-to-one class="Entities.Product" fetch="select" insert="false" name="Product" update="false">
      <column name="idProduct" not-null="true"/>
    </many-to-one>
    <property name="quantity" type="int">
      <column name="quantity" not-null="true"/>
    </property>
    <property name="mode" type="char">
      <column length="1" name="mode" not-null="true"/>
    </property>
    <property name="unity" type="string">
      <column length="25" name="unity" not-null="true"/>
    </property>
    <property name="description" type="string">
      <column length="150" name="description" not-null="true"/>
    </property>
  </class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:1)

复合ID通常意味着列将引用其他表。它们在其他表中自动递增是有意义的,而不是这一个。

如果它们都是自动递增的,那么就不需要复合id - 每个列都能够唯一地标识该行。

如果要使引用的实体具有自动增量值,请检查thisthis个问题(对于基于xml和注释的方法)