Note that there are some explanatory texts on larger screens.

plurals
  1. PODuplicate entries in join table. Using Hibernate, Spring
    primarykey
    data
    text
    <p>I have 3 Tables :</p> <p><strong>Entity</strong></p> <pre><code>CREATE TABLE `entity` ( `entity_number` bigint(20) NOT NULL AUTO_INCREMENT, `title` varchar(6) DEFAULT NULL, `name` varchar(45) NOT NULL, `surname` varchar(45) NOT NULL, `id_number` varchar(13) DEFAULT NULL, `gender` varchar(7) DEFAULT NULL, `age` int(11) DEFAULT NULL, `date_of_birth` datetime DEFAULT NULL, `preferred_language` varchar(10) DEFAULT NULL, `ethnic_group` varchar(15) DEFAULT NULL, `username` varchar(20) DEFAULT NULL, `password` varchar(45) DEFAULT NULL, `status` varchar(10) DEFAULT NULL, PRIMARY KEY (`entity_number`), UNIQUE KEY `id_number_UNIQUE` (`id_number`) ) </code></pre> <p><strong>Contacts</strong></p> <pre><code>CREATE TABLE `contacts` ( `contact_id` int(11) NOT NULL AUTO_INCREMENT, `contact_type` varchar(25) NOT NULL, `value` varchar(100) NOT NULL, PRIMARY KEY (`contact_id`), UNIQUE KEY `contact_id_UNIQUE` (`contact_id`) ) </code></pre> <p><strong>con_contact_entity</strong></p> <pre><code>CREATE TABLE `con_contact_entity` ( `entity_number` bigint(20) NOT NULL, `contact_id` int(11) NOT NULL, PRIMARY KEY (`entity_number`,`contact_id`), KEY `FK_ENTITY_NUMBER` (`entity_number`), KEY `FK_CONTACT_ID` (`contact_id`), CONSTRAINT `FK_CONTACT_ID` FOREIGN KEY (`contact_id`) REFERENCES `contacts` (`contact_id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `FK_ENTITY_NUMBER` FOREIGN KEY (`entity_number`) REFERENCES `entity` (`entity_number`) ON DELETE NO ACTION ON UPDATE NO ACTION ) </code></pre> <p>Then i have my 3 pojos: (Only pasted relevant parts)</p> <p><strong>Entity</strong></p> <pre><code>@javax.persistence.Entity @Table (name = "ENTITY") @Inheritance (strategy = InheritanceType.JOINED) public class Entity { @ManyToMany (targetEntity = Contact.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER) @JoinTable (name = "con_contact_entity", joinColumns = @JoinColumn (name = "entity_number"), inverseJoinColumns = @JoinColumn (name = "contact_id")) private Set&lt;Contact&gt; contactInfo; ... } </code></pre> <p><strong>Contact</strong></p> <pre><code>@XmlAccessorType (XmlAccessType.NONE) @XmlType (name = "Contact", propOrder = {"typeOfContact", "contactValue"}) @Entity @Table (name = "CONTACTS") public class Contact { @Id @GeneratedValue private Long contact_id; @XmlElement (name = "TypeOfContact", required = true, nillable = false) @Column (name = "contact_type") @Enumerated (EnumType.STRING) private ContactType typeOfContact; @XmlElement (name = "ContactValue", required = true, nillable = false) @Column (name = "value") private String contactValue; ... } </code></pre> <p>If i add JOE as an entity with contact details (Type=HomeTel, Value=123456789), and then add JOE's wife who wants to add the same contact details, then there is a duplicate entry in Contact table. How can i make hibernate check for existing duplicate, and if found, just reference those contact details in the con_contact_entity table.</p> <p>Example: result table data would be:</p> <pre><code>**ENTITY** 123 MR JOE .... 124 MRS JANE .... **CON_CONTACT_ENTITY** 123 1 124 1 **CONTACTS** 1 HOMETEL 123456789 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload