title: Hibernate: How return a composite-id?
body: <p>i have this composite-id:</p>
  
  <pre><code>&lt;composite-id class="Entities.PackageId" name="id"&gt;
    &lt;key-property name="id" type="int"&gt;
      &lt;column name="id"/&gt;
    &lt;/key-property&gt;
    &lt;key-property name="idProduct" type="int"&gt;
      &lt;column name="idProduct"/&gt;
    &lt;/key-property&gt;
  &lt;/composite-id&gt;
  </code></pre>
  
  <p>Actually i would like to use auto-increment in package and product id's.
  But looks i cannot do that.</p>
  
  <p>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.</p>
  
  <p>Any ideas who do that ?</p>
  
  <p>Best regards,
  Valter Henrique.</p>
  
  <p>--</p>
  
  <p>DDL:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>The mapping .xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  &lt;!-- Generated 25/02/2011 15:56:00 by Hibernate Tools 3.2.1.GA --&gt;
  &lt;hibernate-mapping&gt;
    &lt;class catalog="p2p" name="Entities.Package" table="Package"&gt;
      &lt;composite-id class="Entities.PackageId" name="id"&gt;
        &lt;key-property name="id" type="int"&gt;
          &lt;column name="id"/&gt;
        &lt;/key-property&gt;
        &lt;key-property name="idProduct" type="int"&gt;
          &lt;column name="idProduct"/&gt;
        &lt;/key-property&gt;
      &lt;/composite-id&gt;
      &lt;many-to-one class="Entities.User" fetch="select" name="User"&gt;
        &lt;column length="50" name="email" not-null="true"/&gt;
      &lt;/many-to-one&gt;
      &lt;many-to-one class="Entities.Product" fetch="select" insert="false" name="Product" update="false"&gt;
        &lt;column name="idProduct" not-null="true"/&gt;
      &lt;/many-to-one&gt;
      &lt;property name="quantity" type="int"&gt;
        &lt;column name="quantity" not-null="true"/&gt;
      &lt;/property&gt;
      &lt;property name="mode" type="char"&gt;
        &lt;column length="1" name="mode" not-null="true"/&gt;
      &lt;/property&gt;
      &lt;property name="unity" type="string"&gt;
        &lt;column length="25" name="unity" not-null="true"/&gt;
      &lt;/property&gt;
      &lt;property name="description" type="string"&gt;
        &lt;column length="150" name="description" not-null="true"/&gt;
      &lt;/property&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>