Spring数据jpa一对一可选共享主键

时间:2017-12-14 17:17:49

标签: hibernate spring-data-jpa

我不认为我的情况很不寻常,但也许我错过了一些东西。这是数据库设置:

Table: proposalitems
Columns:
PK_ProposalRevisionItemID int(10) UN AI PK 
FK_ProposalID int(10) UN 
FK_RevisionID varchar(2) 
ItemID int(10) UN 
ItemText text 
Delivery varchar(9) 
Qty smallint(5) UN 
PriceEach decimal(10,2) UN 
LikelihoodOfSale tinyint(3) UN 
FK_MfgTimeID int(10) UN

CREATE TABLE `proposalitems` (
  `PK_ProposalRevisionItemID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `FK_ProposalID` int(10) unsigned NOT NULL,
  `FK_RevisionID` varchar(2) NOT NULL,
  `ItemID` int(10) unsigned NOT NULL,
  `ItemText` text,
  `Delivery` varchar(9) DEFAULT 'Standard',
  `Qty` smallint(5) unsigned DEFAULT '1',
  `PriceEach` decimal(10,2) unsigned DEFAULT '0.00',
  `LikelihoodOfSale` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `FK_MfgTimeID` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`PK_ProposalRevisionItemID`),
  KEY `FK_MfgTime_idx` (`FK_MfgTimeID`),
  KEY `FK_ItemPropRevID_idx` (`FK_ProposalID`,`FK_RevisionID`),
  CONSTRAINT `FK_ItemPropRevID` FOREIGN KEY (`FK_ProposalID`, `FK_RevisionID`) REFERENCES `proposalrevisions` (`FK_ProposalID`, `FK_RevisionID`) ON DELETE CASCADE ON UPDATE NO ACTION,
  CONSTRAINT `FK_MfgTime` FOREIGN KEY (`FK_MfgTimeID`) REFERENCES `proposalitemmnfgtimes` (`PK_ItemMnfgTimeID`)
) ENGINE=InnoDB AUTO_INCREMENT=6161 DEFAULT CHARSET=utf8;

提案名是父表。

Table: proposalexpdelivery
Columns:
FK_ProposalRevisionItemID int(10) UN PK 
DeliveryTime tinyint(3) UN 
FK_DelUnitID int(10) UN 
FK_DeliveryClauseID int(10) UN

CREATE TABLE `proposalexpdelivery` (
  `FK_ProposalRevisionItemID` int(10) unsigned NOT NULL,
  `DeliveryTime` tinyint(3) unsigned NOT NULL DEFAULT '4',
  `FK_DelUnitID` int(10) unsigned DEFAULT NULL,
  `FK_DeliveryClauseID` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`FK_ProposalRevisionItemID`),
  KEY `FK_ExpDelUnitID` (`FK_DelUnitID`),
  KEY `FK_ExpDeliveryClauseID` (`FK_DeliveryClauseID`),
  CONSTRAINT `FK_ExpDelUnitID` FOREIGN KEY (`FK_DelUnitID`) REFERENCES `units` (`PK_UnitID`),
  CONSTRAINT `FK_ExpDeliveryClauseID` FOREIGN KEY (`FK_DeliveryClauseID`) REFERENCES `proposaldeliveryclause` (`PK_DeliveryClauseID`),
  CONSTRAINT `FK_expPropRevItemID` FOREIGN KEY (`FK_ProposalRevisionItemID`) REFERENCES `proposalitems` (`PK_ProposalRevisionItemID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

所以基本上我有一个提案项目表。每个项目可能有也可能没有加急交付。如果是,则使用与proposalitems表中相同的ID将加速的交货记录保存在proposalexpdelivery表中。我看到的大多数例子都有一对一的关系,如果一个存在,另一个也必须存在。在我的情况下,每个项目都没有加急的交付记录。

我只关心从提案项目中访问加急的交付数据。无需访问加急交付记录,从中获取提案项目数据。

以下是ProposalItem的类:

@Entity
@Table(name="sales.proposalitems", uniqueConstraints=@UniqueConstraint(columnNames={"fk_proposalid","fk_revisionid","itemid"}))
public class ProposalItem implements Serializable {
    ...
    private int proposalRevisionItemID;
    ...
    private ProposalExpeditedDelivery expeditedDelivery;
    ...
    @Id
    @Column(name="pk_proposalrevisionitemid")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonView(View.SimpleProposalView.class)
    public int getProposalRevisionItemID() {
        return proposalRevisionItemID;
    }

    public void setProposalRevisionItemID(int proposalRevisionItemID) {
        this.proposalRevisionItemID = proposalRevisionItemID;
    }
    ...
//    @OneToOne(optional=true,cascade={CascadeType.PERSIST,CascadeType.REMOVE}, orphanRemoval=true, mappedBy="proposalItem", fetch=FetchType.LAZY)
//    @JoinColumn(name="pk_proposalrevisionitemid", referencedColumnName="fk_proposalrevisionitemid")
//    @OneToOne(cascade=CascadeType.ALL, mappedBy="proposalItem")
//    @Transient
//    @OneToOne(optional=true,cascade={CascadeType.ALL}, orphanRemoval=true, fetch=FetchType.LAZY)
//    @JoinColumn(name="pk_proposalrevisionitemid", referencedColumnName="fk_proposalrevisionitemid")
    public ProposalExpeditedDelivery getExpeditedDelivery() {
        return this.expeditedDelivery;
    }

    public void setExpeditedDelivery(ProposalExpeditedDelivery expeditedDelivery) {
        this.expeditedDelivery = expeditedDelivery;
    }
    ...
}

对于加急交付班:

@Entity
@Table(name="sales.proposalexpdelivery")
public class ProposalExpeditedDelivery implements Serializable {
    ...
    private int proposalRevisionItemID;
    private ProposalItem proposalItem;
    ...
    @Id
    @JoinColumn(name="fk_proposalrevisionitemid")
    public int getProposalRevisionItemID() {
        return proposalRevisionItemID;
    }

    public void setProposalRevisionItemID(int proposalRevisionItemID) {
        this.proposalRevisionItemID = proposalRevisionItemID;
    }

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="fk_proposalrevisionitemid")
    @MapsId
    public ProposalItem getProposalItem() {
        return proposalItem;
    }

    public void setProposalItem(ProposalItem proposalItem) {
        this.proposalItem = proposalItem;
    }
    ...
}

我尝试过各种各样的事情。我可以成功检索加急的交付数据,但是一旦我想要更新或添加,它就不会起作用。根据我当时的注释,我会得到各种各样的错误。

我有提案项的存储库和服务层。如果级联工作正常,我认为我不需要那些快速交付,但也许我错了。

在我的测试中,我尝试设置项目中的投放和投放中的项目,并且我已尝试为投放创建存储库,然后在保存项目之前保存投放,但我尝试过的任何工作都没有。

我希望看到一个明确的示例,说明要使用哪些注释,理想情况下如何设置测试以添加和编辑项目的投放,但我会对正确的注释感到满意。

我正在使用Spring Boot,它正在使用Hibernate 5.2.12。

另外,我相信我的数据库结构设置正确且有效(我正在使用MySQL),但如果有更好的方法来实现我的需要,我可以根据需要重组数据库。

修改

我测试它的代码有一个现有的ProposalItem对象。然后我创建一个新的ProposalExpeditedDelivery对象并尝试保存ProposalItem。

ProposalExpeditedDelivery ped = new ProposalExpeditedDelivery();
ped.setDeliveryTime(4);
ped.setDeliveryUnit(unit);
ped.setDeliveryClause(clause);
//ped.setProposalItem(proposalItem);
proposalItem.setExpeditedDelivery(ped);

//expeditedDeliveryRepository.save(ped);
proposalItemRepository.save(proposalItem);

像这样(并使用egallardo的答案)我得到错误:

org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing

如果我取消注释该行,以便它首先尝试保存ProposalExpeditedDelivery对象,则会收到错误:

attempted to assign id from null one-to-one property

如果我取消注释上面的两个注释行,我会收到错误:

detached entity passed to persist

1 个答案:

答案 0 :(得分:1)

删除额外的JoinColumn,只考虑对象(而不是数据库键):

@Entity
@Table(name="sales.proposalexpdelivery")
public class ProposalExpeditedDelivery implements Serializable {
    ...
    private ProposalItem proposalItem;
    ...

    @Id
    @GenericGenerator(name = "generator", strategy = "foreign",  parameters = @Parameter(name = "property", value = "proposalItem"))
    @GeneratedValue(generator = "generator")
    @JoinColumn(name="fk_proposalrevisionitemid", unique=true, nullable=false)
    public int getProposalRevisionItemID() {
        return proposalRevisionItemID;
    }

    @OneToOne(fetch=FetchType.LAZY)
    @PrimaryKeyJoinColumn
    public ProposalItem getProposalItem() {
        return proposalItem;
    }

    public void setProposalItem(ProposalItem proposalItem) {
        this.proposalItem = proposalItem;
    }
    ...
}

父实体应如下所示:

@Entity
@Table(name="sales.proposalitems", uniqueConstraints=@UniqueConstraint(columnNames={"fk_proposalid","fk_revisionid","itemid"}))
public class ProposalItem implements Serializable {
    ...
    private int proposalRevisionItemID;
    ...
    private ProposalExpeditedDelivery expeditedDelivery;
    ...
    @Id
    @Column(name="pk_proposalrevisionitemid", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonView(View.SimpleProposalView.class)
    public int getProposalRevisionItemID() {
        return proposalRevisionItemID;
    }

    public void setProposalRevisionItemID(int proposalRevisionItemID) {
        this.proposalRevisionItemID = proposalRevisionItemID;
    }
    ...
    @OneToOne(fetch = FetchType.LAZY,cascade=CascadeType.ALL,mappedBy="proposalItem")
    public ProposalExpeditedDelivery getExpeditedDelivery() {
        return this.expeditedDelivery;
    }

    public void setExpeditedDelivery(ProposalExpeditedDelivery expeditedDelivery) {
        this.expeditedDelivery = expeditedDelivery;
    }
    ...
}

测试代码:

retrieve proposalItem;

ProposalExpeditedDelivery ped = proposalItem.getExpeditedItem();
if(ped == null){
  ped = new ProposalExpeditedDelivery();
  ped.setProposalItem(proposalItem);
  proposalItem.setExpeditedItem(ped);
  expeditedDeliveryRepository.save(ped);
}

ped.setDeliveryTime(4);
ped.setDeliveryUnit(unit);
ped.setDeliveryClause(clause);

proposalItemRepository.save(proposalItem);

在提案项的服务层上,这是更新代码:

@Override
public ProposalItem update(ProposalItem entity) throws EntityNotFoundException {
    ProposalItem ent = null;
    try {
        ent = repository.findById(entity.getProposalRevisionItemID()).get();
    } catch (Exception ex) {
        LOGGER.error(ex.getMessage());
        throw new EntityNotFoundException();
    }
    if (ent == null) {
        throw new EntityNotFoundException();
    }
    // get the expedited delivery object from the existing database entry
    ProposalExpeditedDelivery ped = ent.getExpeditedDelivery();
    // if the existing database entry is null but the update client object does contain a ProposalExpeditedDelivery object
    if (ped==null && entity.getExpeditedDelivery()!=null) {
        entity.getExpeditedDelivery().setProposalItem(ent);
        ent.setExpeditedDelivery(entity.getExpeditedDelivery());
        expeditedDeliveryRepository.save(ent.getExpeditedDelivery());
    }
    // if the existing database entry has an expedited delivery object, but the client object does not
    else if (ped!=null && entity.getExpeditedDelivery()==null) {
        expeditedDeliveryRepository.delete(ped);
    } 
    // if both the existing database entry and the client entry have an expedited delivery object, but they are not equal
    else if (ped!=null && !ent.getExpeditedDelivery().equals(entity.getExpeditedDelivery())) {
        ped.setDeliveryTime(entity.getExpeditedDelivery().getDeliveryTime());
        ped.setDeliveryUnit(entity.getExpeditedDelivery().getDeliveryUnit());
        ped.setDeliveryClause(entity.getExpeditedDelivery().getDeliveryClause());
        expeditedDeliveryRepository.save(ent.getExpeditedDelivery());
    }
    entity = setupForUpdate(entity);
    if (entity.getExpeditedDelivery()!=null) {
        entity.setExpeditedDelivery(ent.getExpeditedDelivery());
    }
    return repository.save(entity);
}

更新的逻辑基本上是:

  1. 接收客户对象
  2. 从DB
  3. 加载现有对象
  4. 更改/更新对象属性
  5. 保存更改