gpt4 book ai didi

hibernate - spring data jpa一对一可选共享主键

转载 作者:行者123 更新时间:2023-12-04 03:07:12 25 4
gpt4 key购买 nike

我不认为我的情况不寻常,但也许我遗漏了什么。这是数据库设置:

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;

proposalitems 是父表。

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;

基本上我有一个提案项目表。每件商品可能有也可能没有加急交货。如果是,加急递送记录将保存在 proposalexpdelivery 表中,使用与 proposalitems 表中相同的 id。我看到的大多数例子都有一对一的关系,如果一个存在,另一个也必须存在。就我而言,不必为每件商品都提供加急配送记录。

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

这是 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

最佳答案

删除额外的 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);

在 Proposal Items 的服务层,这里是更新代码:

@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. 从数据库中加载现有对象
  3. 更改/更新对象属性
  4. 保存更改

关于hibernate - spring data jpa一对一可选共享主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47818876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com