title: Problem with JPA Mapping One-to-Many with jointable
body: <p>I have a JPA Mapping question to do.</p>
  
  <p>We have a One-To-Many relationship between two entities (Sale and Pig). Classes follow at the final of this message to ilustrate.</p>
  
  <p>'Sale' is a event on a 'Pig', like many others in the system ('Inspection' is another example).
  However, 'Sale' is the only event who has One-To-Many relationship with 'Pig', the others has One-to-One.</p>
  
  <p>So, to map all events of a 'Pig' we use a 'PigEvent' entity.
  We save(insert) a 'PigEvent' object at the same time the user inserts a 'Pig' in the system.</p>
  
  <p>We want to use this entity ('PigEvent') like the 'jointable' of Sale.getPigs() mapping.
  But doing that, some problem occurs:
        - when a new 'Sale' is inserted, hibernate try to insert new 'PigEvent' for every 'Pig' in the 'Sale'
              (this generates a duplicate PK exception, because PigEvent already exists)
        - when a new 'Sale' is deleted, hibernate deletes 'PigEvent' for every 'Pig' in the 'Sale'
              (doing this we loose the others Events relationship data)</p>
  
  <p>We understand that this is the normal behaviour of this kind of mapping (One-to-Many with jointable).
  We want to know how configurate JPA/Hibernate to just load Sale.getPigs() (in SELECT´s),
  but in INSERT, UPDATE, DELETE operations in 'Sale' don´t action at all in that mapping (Sale.pigs()).</p>
  
  <p>We use Hibernate 3.6.2.</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>@Entity
  public class Pig extends Persistente implements Serializable {}
  
  @Entity
  public class PigEvent extends Persistente {
  @OneToOne
  @JoinColumn(name="idpig")
  private Pig pig;
  
  @OneToOne
  @JoinColumn(name="idapproval")
  private Inspection approval
  
  @OneToOne
  @JoinColumn(name="idsale")
  private Sale sale;
  }
  
  
  @Entity
  public class Inspection extends Persistente{
        @OneToOne
        @JoinColumn(name="idSuino")
        private Pig pig;
  }
  
  
  @Entity
  public class Sale extends Persistente{
  @MOneToMany
  @JoinTable(name="e33PigEvent",uniqueConstraints=@UniqueConstraint(columnNames="idpig"),
        joinColumns={@JoinColumn(name="idsale",insertable=false,updatable=false)},
        inverseJoinColumns={@JoinColumn(name="idpig",insertable=false,updatable=false)})
  public Set&lt;Pig&gt; getPigs() {}
  }
  
  Table Structure:
  CREATE TABLE  `e33Pig` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`id`)
  )
  
  
  CREATE TABLE  `e33PigEvent` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `idPig` int(11) NOT NULL,
    `idInspection` int(11) DEFAULT NULL,
    `idSale` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `idPig` (`idPig`),
    CONSTRAINT `fk_e33eventossuino_e33aprovacao1` FOREIGN KEY (`idInspection`) REFERENCES `e33Inspection` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT `fk_e33eventossuino_e33suino1` FOREIGN KEY (`idPig`) REFERENCES `e33Pig` (`id`),
    CONSTRAINT `fk_e33eventossuino_e33venda1` FOREIGN KEY (`idSale`) REFERENCES `e33Sale` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
  );
  
  CREATE TABLE  `e33Sale` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`id`)
  );
  
  CREATE TABLE  e33Inspection (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `idsuino` int(11) NOT NULL,
    PRIMARY KEY (`id`),
    CONSTRAINT `fk_e33Inspection_e33suino1` FOREIGN KEY (`idPig`) REFERENCES `e33Pig` (`id`)
  ) ;
  </code></pre>