Home > Mobile >  Create JPA entity without database connection
Create JPA entity without database connection

Time:10-20

I am trying to create a new JPA entity without generating a new row in the database.

I have already tried to copy an existing entity, but the problem is that the copy seems to have a connection to the original (I guess because of the ID). Because when I try to set new values, the values are set for both (copy and original).

The problem is: I have a list of objects (the JPA entities) from this list, some objects need to be added together. The sum of the added objects is to be displayed in a new object - but this object must not appear in the database.

This is what the Entity looks like:

@Aggregate
@Entity
@Table(name = "CUSTOMER_PORTFOLIO_UE",
    indexes = {
            @Index(columnList = "isin"),
            @Index(columnList = "clearstreamDepotNumber"),
            @Index(columnList = "validFromDate"),
            @Index(columnList = "isin, clearstreamDepotNumber, validFromDate")
    })
public class CustomerPortfolioUpdateEvent extends AbstractUpdateEvent implements HasLogicalKey<LogicalCustomerPortfolioKey> {

@NotEmpty
@Length(max = 7)
@Column(nullable = false, length = 7)
private String customerAccountNumber;
@NotNull
@Length(max = 3)
@Column(nullable = false, length = 3)
private String portfolioId;
@NotNull
@Length(min = 12, max = 12)
@Column(nullable = false, length = 12)
private String isin;
@NotEmpty
@Length(max = 7)
@Column(nullable = false, length = 7)
private String depotNumber;
@Nullable
@Length(max = 20)
@Column(length = 20)
private String customerAccountShortName;
@Nullable
@Length(max = 1)
@Column(length = 1)
private String customerGroupId;
@NotNull
@Column(nullable = false, length = 4)
private LegalEntity legalEntity;
@Nullable
@Length(max = 6)
@Column(length = 6)
private String customerSectorId;
@Nullable
@Length(max = 20)
@Column(length = 20)
private String customerSectorName;
@Nullable
@Length(max = 8)
@Column(length = 8)
private String customerAdvisorNumber;
@Nullable
@Length(max = 20)
@Column(length = 20)
private String customerAdvisorName;
@NotNull
@Length(min = 2, max = 3)
@Column(nullable = false, length = 3)
private String taxCountryId;
@NotNull
@Length(min = 2, max = 3)
@Column(nullable = false, length = 3)
private String countryId;
@Nullable
@Length(max = 20)
@Column(length = 20)
private String depotName;
@Nullable
@Length(max = 20)
@Column(length = 20)
private String clearstreamDepotNumber;
@NotNull
@Column(nullable = false, columnDefinition = "DATE")
private LocalDate validFromDate;
@Nullable
@Column(precision = 18, scale = 3)
private BigDecimal openPositionValue;
@Nullable
@Column(precision = 18, scale = 3)
private BigDecimal settledPositionValue;
@NotNull
@Column(nullable = false, precision = 18, scale = 3)
private BigDecimal tradingPositionValue;
@Nullable
@Length(max = 3)
@Column(length = 3)
private String isinSub;


protected CustomerPortfolioUpdateEvent() { }

public CustomerPortfolioUpdateEvent(
        @NotNull Timestamp recordedAt,
        @NotNull @Length(min = 7, max = 7) String customerAccountNumber,
        @NotNull @Length(max = 3) String portfolioId,
        @NotNull @Length(min = 12, max = 12) String isin,
        @NotNull @Length(max = 7) String depotNumber,
        @Nullable String customerAccountShortName,
        @Nullable String customerGroupId,
        @NotNull LegalEntity legalEntity,
        @Nullable @Length(max = 6) String customerSectorId,
        @Nullable @Length(max = 20) String customerSectorName,
        @Nullable @Length(max = 8) String customerAdvisorNumber,
        @Nullable @Length(max = 20) String customerAdvisorName,
        @NotNull @Length(min = 2, max = 3) String taxCountryId,
        @NotNull @Length(min = 2, max = 3) String countryId,
        @Nullable @Length(max = 20) String depotName,
        @Nullable @Length(max = 20) String clearstreamDepotNumber,
        @NotNull LocalDate validFromDate,
        @Nullable BigDecimal openPositionValue,
        @Nullable BigDecimal settledPositionValue,
        @NotNull BigDecimal tradingPositionValue,
        @Nullable String isinSub
        ) {
    super(recordedAt);
    this.customerAccountNumber = customerAccountNumber;
    this.portfolioId = portfolioId;
    this.isin = isin;
    this.depotNumber = depotNumber;
    this.customerAccountShortName = customerAccountShortName;
    this.customerGroupId = customerGroupId;
    this.legalEntity = legalEntity;
    this.customerSectorId = customerSectorId;
    this.customerSectorName = customerSectorName;
    this.customerAdvisorNumber = customerAdvisorNumber;
    this.customerAdvisorName = customerAdvisorName;
    this.taxCountryId = taxCountryId;
    this.countryId = countryId;
    this.depotName = depotName;
    this.clearstreamDepotNumber = clearstreamDepotNumber;
    this.validFromDate = validFromDate;
    this.openPositionValue = openPositionValue;
    this.settledPositionValue = settledPositionValue;
    this.tradingPositionValue = tradingPositionValue;
    this.isinSub = isinSub;
}

CodePudding user response:

if you are copying the jpa entity, you can try with a new JpaRepository Interface the new Entity is referring to a new Table or same Table

CodePudding user response:

If you want to have a copy of your entity, first load it and then detach it.

EntityManager em;
//load entity. for example :
A a = em.find(A.class, 39L);
//then detach
em.detach(a)
//you might want to clear the id:
a.setId(null);
//change some values if you want
a.setLabel("foo");
//persist the copy if you want
em.persist(a)
  • Related