Свойство [столбца] объекта [субъекта] было изменено, но оно не будет обновлено, потому что свойство неизбежноJAVA

Программисты JAVA общаются здесь
Anonymous
Свойство [столбца] объекта [субъекта] было изменено, но оно не будет обновлено, потому что свойство неизбежно

Сообщение Anonymous »

Это было задано ранее, но я не нашел никаких ответов, которые работают для моего варианта использования.
Я создал упрощенный пример игрушек, чтобы объяснить мою проблему.
Представьте себе, что у нас есть клиент < /code> и покупка < /code> entity: < /p>

Код: Выделить всё

@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
с данными p2 , но проблема в том, что в моем фактическом коде у покупок есть много полей, и я хочу избежать ручного настройки каждого из них, если это возможно.>

Подробнее здесь: https://stackoverflow.com/questions/730 ... t-be-updat

Вернуться в «JAVA»