@ManyToOne (updatable = false) - how should it work?

I want to have read-only functionality in one of my objects. I know that in JPA 2.0 we do not have such functionality as such. I thought we could achieve this using updateable=false, insertable=false , but I don't think I'm getting how it works.

Suppose I have two objects: OrderedItem and Customer :

 @Entity public class OrderedItem { @Id @GeneratedValue private int id; private String name; @ManyToOne @JoinColumn(updatable = false) private Customer owner; // bunch of simple getters and setters } @Entity public class Customer { @Id @GeneratedValue private int id; private String name; @OneToMany(mappedBy="owner") private Set<OrderedItem> orderedItems; // bunch of simple getters and setters } 

Now consider the following code:

 Customer john = new Customer(); john.setName("John"); OrderedItem milk = new OrderedItem(); milk.setName("Milk"); milk.setOwner(john); Set<OrderedItem> items = new HashSet<OrderedItem>(); items.add(milk); john.setOrderedItems(items); // This starts the EM transaction startTx(); em.persist(john); em.persist(milk); stopTx(); startTx(); OrderedItem milkFromPC = em.find(OrderedItem.class, milk.getId()); System.out.println(milkFromPC.getName() + " ordered by customer: " + milkFromPC.getOwner().getName()); // Changing the state of Owner entity through the OrderedItem milkFromPC.getOwner().setName("Terrence"); stopTx(); 

Now, without @JoinColumn(updatable = false) in the OrderedItem entity, the OrderedItem would be retrieved from the PC, I would have access to its owner - a Customer - and successfully change its name. This would not be a surprise, because Customer also in a managed state, so it had to be reflected in the database.

However , I assumed that updateable=false in @JoinColumn set on one side of the relationship would prevent the SQL UPDATE statement from occurring. Unfortunately, at the end I can see the name changed in the database (this is Terrence, not John). I can also see the executed SQL UPDATE query:

[EL Fine]: 2011-11-30 23: 41: 27,941 - ClientSession (16862753) - Connection (11024915) - thread (thread [main, 5, main]) - CUSTOMER NAME UPDATE =? WHERE (ID =?) Bind => [Terrence, 1]

So - what does this updateable=false really do? Why do I need it? Does my foreign key protect against change? It is like "you cannot change the entity, but you can change the state of an object"?

+4
source share
1 answer

From the documentation

Whether the column is included in SQL UPDATE statements created by the save provider.

So, as you said, "it only protects a changing foreign key"

+3
source

Source: https://habr.com/ru/post/1384000/


All Articles