gpt4 book ai didi

java - 与连接表的一对多关系 : relation not being persisted

转载 作者:行者123 更新时间:2023-12-01 12:32:08 27 4
gpt4 key购买 nike

我正在使用 hibernate JPA 注释,至于存储库,我正在使用 Spring JPA

我有以下两个实体:

@Entity
@Table(name = "idn_organization")
@Audited
public class Organization extends UuidBasedEntity {
@Column(name = "full_name", length = 64, nullable = false)
private String fullName;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinTable(
name = "idn_organization_address",
joinColumns = @JoinColumn(name = "organization_id"),
inverseJoinColumns = @JoinColumn(name = "address_id")
)
private Set<Address> addresses;

@Column(name = "phone_number", length = 12, nullable = true)
private String phoneNo;

@Column(name = "registered_date", nullable = true)
private Date registeredDate;

@Column(name = "social_security", length = 9, nullable = true)
private String socialSecurity;

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public Set<Address> getAddresses() {
return addresses != null ? addresses : new HashSet<Address>();
}

public void setAddresses(Set<Address> addresses) {
if (this.addresses == null)
this.addresses = new HashSet<>();

this.addresses.clear();

if (addresses != null)
this.addresses.addAll(addresses);
}

还有:

@Entity
@Table(name = "cmn_address")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Audited
public class Address extends AutoIdBasedEntity {

@Valid
@NotNull(message = "{address.type.notNull}")
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "type_code")
@ForeignKey(name = "FK_address_address_type")
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private AddressType type;

@NotNull(message = "{address.line1.notNull}")
@Column(name = "address_line_1", length = 256, nullable = true)
private String addressLine1;

@Column(name = "address_line_2", length = 128, nullable = true)
private String addressLine2;

@Column(name = "address_line_3", length = 128, nullable = true)
private String addressLine3;

@Valid
@NotNull(message = "{address.town.notNull}")
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "city_id")
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private Town city;

@NotNull(message = "{address.state.notNull}")
@Size(min = 2, max = 2, message = "{address.state.notEmpty}")
@Column(name = "state_code")
private String state;

//@NotNull(message = "{address.zip.notNull}")
// TODO Not all DTOP Results match
// @Pattern(regexp = "\\d{5}(-\\d{4})?", message = "{address.zip.notValid}")
@Column(name = "postal_code", length = 32, nullable = true)
private String postalCode;

@Valid
@NotNull(message = "{address.country.notNull}")
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "country_code")
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private Country country;

如上所述,我的存储库是:

public interface OrganizationRepository extends JpaRepository<Organization, String>, JpaSpecificationExecutor<Organization> {
}

我遇到的问题特别是在我的服务中保存的方法:

@Service("identityService")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class IdentityServiceImpl implements IdentityService {
private static Logger logger = LoggerFactory.getLogger(IdentityServiceImpl.class);

@Autowired
private OrganizationRepository organizationRepository;

@Override
public void persistOrganization(Organization organization) {
organizationRepository.save(organization);
if (logger.isDebugEnabled()) {
logger.debug("Organization [" +
organization.getUuid() + " - " +
organization.getFullName() + "] created or updated.");
}
}
}

每当我调用保存方法时,组织都会被持久化,它们各自表中的地址也会被持久化......但连接表条目却不会!因此,我获得了一个新的组织、一个新的地址,但它们之间没有任何联系。那么我在这里做错了什么?当我尝试以某种方式编辑组织时,显然也会发生这种情况。

最佳答案

问题出在我的服务中,因为我有注释:

@Transactional(readOnly = true, propagation = Propagation.REQUIRED)

并且因为我通过网络流管理事务,所以它们从未提交。因此,我将 @Transactional 添加到持久方法中并修复了它。您也可以删除 readOnly = true 部分,但我没有这样做,因为有一些我不想公开的 fetch 方法。

关于java - 与连接表的一对多关系 : relation not being persisted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25854916/

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