Jessy Chenavas (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiZGQyNmY2Y2I5...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16026?atlOrigin=eyJpIjoiZGQyNm...
) HHH-16026 (
https://hibernate.atlassian.net/browse/HHH-16026?atlOrigin=eyJpIjoiZGQyNm...
) @OrderColumn creating too many (useless) update queries (
https://hibernate.atlassian.net/browse/HHH-16026?atlOrigin=eyJpIjoiZGQyNm...
)
Issue Type: Bug Affects Versions: 6.1.6 Assignee: Unassigned Components: hibernate-core
Created: 11/Jan/2023 11:01 AM Priority: Minor Reporter: Jessy Chenavas (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3...
)
Hello, recently we started to work with the @OrderColumn feature and found out that by
using this the entityManager is creating a lot of update queries where it would not be
necessary to have them.
Let's take this example:
@Entity
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", orphanRemoval =
true, fetch = FetchType.EAGER)
@OrderColumn(name = "pos")
private List<Child> childs;
...
}
@Entity
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private int pos;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "parent_id")
private Parent parent;
...
}
We run the following code:
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
session.save(new Parent(Arrays.asList(new Child(), new Child())));
tx.commit();
sessionFactory.close();
And we can see this output in the logs:
Hibernate: insert into Parent (id) values (default)
Hibernate: insert into Child (id, parent_id, pos) values (default, ?, ?)
Hibernate: insert into Child (id, parent_id, pos) values (default, ?, ?)
Hibernate: update Child set pos=? where id=?
Hibernate: update Child set pos=? where id=?
Why does hibernate need to set the position using a separate query, getting the position
is very straightforward (basically just an indexOf on the bag). So why do we have two
requests, a first insert with the position set to 0, and then an update to set the right
position?
In that case it’s not that important, but we end up having performances slowed because of
this when saving a lot of entities.
(
https://hibernate.atlassian.net/browse/HHH-16026#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16026#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100213- sha1:b01baad )