0

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

Bill 和 BillSimpleentry 具有一对一的关系。每张账单都有一个账单简单条目。所以 billsimplementry.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'对象被持久化时,它也应该在数据库中创建一个 billsimpleentry 行。

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) 失败,因为 billsimpleentry.billId 值需要填充为与 bill.id 相同的值。我应该如何解决这个问题?看来我需要更新我的表结构或表自动 id 生成策略。任何见解将不胜感激。

4

1 回答 1

0

抱歉,但我需要更多说明。我不知道您是如何创建该 JSON 的,但其中有这个"billId":null。您需要设置父 ID。因此,如果那是 JSON,请在持久化之前对其进行处理并将父 ID 附加到它。保存或持久化不应自动将父 ID 附加到子代。

于 2013-10-25T17:37:27.120 回答