Die [Spalte] Eigenschaft der [Entität] -Entität wurde geändert, wird jedoch nicht aktualisiert, da die Eigenschaft unver
Posted: 05 Apr 2025, 12:39
Dies wurde zuvor gefragt, aber ich habe keine Antworten gefunden, die für meinen Anwendungsfall funktionieren.
Ich habe ein vereinfachtes Spielzeugbeispiel erstellt, um mein Problem zu erklären. mit den Daten von P2 , aber das Problem ist, dass in meinem tatsächlichen Code Käufe viele Felder haben und ich es vermeiden möchte, jeden einzelnen manuell einzustellen.>
Ich habe ein vereinfachtes Spielzeugbeispiel erstellt, um mein Problem zu erklären.
Code: Select all
@Entity
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
protected Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
< /code>
@Entity
public class Purchase {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(name = "item_name")
private String itemName;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@ManyToOne
@JoinColumns({
@JoinColumn(updatable=false, insertable=false, name="first_name", referencedColumnName="first_name"),
@JoinColumn(updatable=false, insertable=false, name="last_name", referencedColumnName="last_name")
})
Customer customer;
public Customer getCustomer() { return customer; }
public void setCustomer(Customer customer) { this.customer = customer; }
public void setID(Long id) { this.id = id; }
public Long getID() { return this.id; }
protected Purchase() { }
public Purchase(String itemName, String firstName, String lastName) {
this.itemName = itemName;
this.firstName = firstName;
this.lastName = lastName;
}
}
< /code>
As you can see, purchases are joined to customers using two columns: the first name of the customer, and last name (I know this is somewhat contrived since it would be better to join on customer id).
The issue is when I try to update an existing purchase. If the purchase details change, I create an entirely new purchase, set this new purchases's ID field, and save the new purchase which overwrites the old one since they have the same ID.
customerRepository.save(new Customer("firstName", "lastName"));
purchaseRepository.save(new Purchase("itemName", "firstName", "lastName"));
Purchase p1 = purchaseRepository.findAll().get(0);
Purchase p2 = new Purchase("updatedItemName", "firstName", "lastName");
p2.setID(p1.getID());
purchaseRepository.save(p2);
< /code>
BUT, this always produces this warning:
2022-07-22 21:04:22.088 WARN 46139 --- [ main] o.h.p.entity.AbstractEntityPersister : HHH000502: The [customer] property of the [com.example.accessingdatajpa.Purchase] entity was modified, but it won't be updated because the property is immutable.
< /code>
I need help preventing this warning.
I know I could just update p1