开发者

Hibernate: How return a composite-id?

i have this composite-id:

<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>

Actually i would like to use auto-increment in package and product id's. But looks i cannot do that.

So, i have to generate one id to package, (product already has auto-increment key), so my id is take the max value from package.id + 1 , so this way i have a new id to my package.

Any ideas who do that ?

Best regards, 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;

The mapping .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-n开发者_JS百科ull="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>


The composite ID usually means that the columns will have references to other tables. It makes sense for them to be auto-incremented in the other tables, not this one.

And if they both were auto-incremented, then there wouldn't be a need for a composite id - each one of the columns will be able to uniquely identify the row.

If you want to make the referenced entities have autoincremented values, check this and this questions (for xml and annotation-based approaches)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜