持久化实体与它们之间只有一对一关系的JPA与Hibernate问题

时间:2013-10-25 16:58:39

标签: mysql sql hibernate jpa orm

我有两张桌子 - bill& billSimpleentry和两个相应的实体类Bill& BillSimpleEntry。

Bill和BillSimpleentry有一对一的关系。每个法案都有一个单一的法案。因此,billimplementry.billId具有与bill.id相同的对应值。

SQL结构:

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 */;

因此,当一个新的'bill'对象被持久化时,它还应该在数据库中创建一个billimpleentry行。

save(Bill newBill){
em.persist(newBill);
}

比尔类结构:

@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 & setters...
}

BillSimpleEntry:

@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<SimpleUserIdAndLiableCost> simpleUserIdAndLiableCost = new ArrayList<SimpleUserIdAndLiableCost>();
        ... getters & setters...

}

以下是尝试保留的newBill obj数据

{
   "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
}

但问题是em.persist(Bill)失败了,因为需要将billimpleentry.billId值填充到与bill.id相同的值。我该如何解决这个问题?看起来我需要更新我的表结构或表自动id生成策略。任何见解都将不胜感激。

1 个答案:

答案 0 :(得分:0)

很抱歉,但我需要进一步澄清。我不知道你是如何创建那个JSON的,但是在它们中是“billId”:null 。您需要设置父ID。因此,如果这是JSON,请在持久化之前处理它并将父ID附加到它。保存或保留不应自动将父ID附加到子项。