Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA with Hibernate issue when persisting Entities with one-one relationship between them
    primarykey
    data
    text
    <p>I have two tables - bill &amp; billSimpleentry and two corresponding Entity classes Bill &amp; BillSimpleEntry.</p> <p>Bill and BillSimpleentry have a one-one relationship. Each bill has one billsimpleentry. So billsimplementry.billId has the same corresponding value of bill.id.</p> <p>SQL structure:</p> <pre><code>CREATE TABLE `bill` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `title` varchar(100) DEFAULT NULL, ..... ..... PRIMARY KEY (`id`), UNIQUE KEY `id_UNIQUE` (`id`), KEY `fk_bill_groups1_idx` (`groupId`), KEY `fk_bill_user1_idx` (`billPayerId`), CONSTRAINT `fk_bill_groups` FOREIGN KEY (`groupId`) REFERENCES `groups` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `fk_bill_user` FOREIGN KEY (`billPayerId`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; CREATE TABLE `billsimpleentry` ( `itemTitle` varchar(200) DEFAULT NULL, `itemDescription` text, `billId` bigint(20) NOT NULL, PRIMARY KEY (`billId`), KEY `fk_bill_idx` (`billId`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; </code></pre> <p>So when a new 'bill' object is persisted, it should also create a billsimpleentry row in the database.</p> <pre><code>save(Bill newBill){ em.persist(newBill); } </code></pre> <p>Bill class structure:</p> <pre><code>@Entity @Table(name = "bill") public class Bill implements GenericObject { private static final long serialVersionUID = -5660869020353250221L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String title; .... private Long groupId; private BigDecimal billTotal; @OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER) @PrimaryKeyJoinColumn private BillSimpleEntry billSimpleEntry; ... getters &amp; setters... } </code></pre> <p>BillSimpleEntry:</p> <pre><code>@Entity @Table(name="billsimpleentry") public class BillSimpleEntry implements GenericObject{ @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long billId; @Column(columnDefinition="TEXT") private String itemDescription; @OneToMany(cascade=CascadeType.ALL,mappedBy="billSimpleEntryId",fetch = FetchType.EAGER) private List&lt;SimpleUserIdAndLiableCost&gt; simpleUserIdAndLiableCost = new ArrayList&lt;SimpleUserIdAndLiableCost&gt;(); ... getters &amp; setters... } </code></pre> <p>Here is the newBill obj data that is attempted to be persisted</p> <pre><code>{ "id":null, "title":"", "billDate":null, "billPayerId":6, "notes":null, "billCreaterId":null, "groupId":3, "billTotal":null, "billSimpleEntry":{ "billId":null, "itemDescription":null, "simpleUserIdAndLiableCost":[ { "userId":6, "liableCost":"50", "id":null, "billSimpleEntryId":null, "user":{ "id":null, "fName":"doe", "lName":"doe" }, "isActive":true }, { "userId":7, "liableCost":"50", "id":null, "billSimpleEntryId":null, "user":{ "id":null, "fName":"doe", "lName":"doe" }, "isActive":true }, { "userId":8, "liableCost":"50", "id":null, "billSimpleEntryId":null, "user":{ "id":null, "fName":"doe", "lName":"doe" }, "isActive":true } ], "itemDescriptionId":2 }, "billItemEntry":[ ], "userId":null } </code></pre> <p>But the problem is that em.persist(Bill) fails because billsimpleentry.billId value needs to be populated to the same value as of bill.id. How should I fix this problem? It appears like I need to update my table structure or the table auto id generation strategy. Any insights would be appreciated.</p>
    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.
 

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