簡體   English   中英

與聯接表一對多的關系:關系不持久

[英]one-to-many relationship with join table: relation not being persisted

我正在使用休眠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.");
        }
    }
}

每當我調用save方法時,組織就會保留下來,其相應表中的地址也會保留下來……但是聯接表項卻不會保留! 因此,我獲得了一個新的組織,一個新的地址,但是它們之間沒有任何聯系。 那我在做什么錯呢? 當我嘗試以某種方式編輯組織時,顯然也會發生這種情況。

問題出在我的服務上,因為我有注釋:

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

因為我是通過Web流管理事務的,所以它們從未提交。 所以我在持久方法中添加了@Transactional ,並對其進行了修復。 您也可以只刪除readOnly = true部分,但是我沒有這樣做,因為有一些我不想公開的獲取方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM