Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6570775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:47:54+00:00 2026-05-25T14:47:54+00:00

I have a JPA Mapping question to do. We have a One-To-Many relationship between

  • 0

I have a JPA Mapping question to do.

We have a One-To-Many relationship between two entities (Sale and Pig). Classes follow at the final of this message to ilustrate.

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

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.

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)

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()).

We use Hibernate 3.6.2.

Thanks in advance.

@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<Pig> 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`)
) ;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:47:54+00:00Added an answer on May 25, 2026 at 2:47 pm

    You can’t use the same table (e33PigEvent) to map an entity (PigEvent) and an association (the OneToMany association). If the table is mapped to an entity, then you don’t have a OneToMany association between Sale and Pig anymore: you have a OneToMany between Sale and PigEvent, mapped by a foreign key in e33PigEvent, and a OneToOne between PigEvent and Pig, also mapped by a foreign key in e33PigEvent.

    If you map a OneToMany using a JoinTable, then Hibernate handles insertions and deletions in this table itself, each time you add or remove Pigs from the collection. Since you have additional columns in the join table, you need to create PigEvent instances yourself, and add these instances to the collection of events of the sale.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to generate a unidirectional one-to-many mapping between two JPA entities. In
I have a question regarding the JPA OR mapping between two persistent entities with
I have a JPA object which has a many-to-many relationship like this: @Entity public
I have a some JPA entities that inherit from one another and uses discriminator
I am learning JPA and have one question: In which situations we need more
I have a hierarchical JPA mapping that is several Classes deep. Something like this
I have a JPA entity class (one of many) and I can run JPQL
I have a question regarding JPA persistence in Glassfish. Situation: I have a Supplier
I have a SEAM app with some JPA/Hibernate entities. And I now wonder where
I have problems in mapping custom collection with JPA (Hiberante provider). For example when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.