JPA OrderColumn null values ​​for merge

I am using JPA 2.0 with Hibernate 4.2.5. I have bi-directional mapping to one type of object.

@OneToMany(mappedBy = "parent", cascade = { CascadeType.PERSIST, CascadeType.DETACH }, orphanRemoval = true, fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@OrderColumn(name = "position", nullable=false)
private List<MenuItem> children = new ArrayList<MenuItem>();

@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinTable(name = "MenuItem_MenuItem", joinColumns = { @JoinColumn(name = "child_id") }, inverseJoinColumns = { @JoinColumn(name = "parent_id") })
private MenuItem parent;

This mapping creates the joined columns parent_id, child_id, and position. When I want to add a child to the parent, I do the following:

MenuItem newItem = service.persist(..); 
parent.getChildren().add(newItem);
newItem.setParent(parent); 
service.merge(newItem);
service.merge(parent);

which generates the following:

Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into MenuItem (menu_id, message, messageId, params, id) values (?, ?, ?, ?, ?)
Hibernate: insert into MenuItem_MenuItem (parent_id, child_id) values (?, ?)

The problem is that the second insertion does not include the position property, so it remains zero. What am I doing wrong?

EDIT I use spring declarative transaction management and all of my merge and save methods are annotated using @Transactional. Could this be a problem?

+3
source share
4 answers

HHH-5732, ​​ 4.3. , , :

, , , , , , MenuItem position , .

, , , , position .

, Hibernate , , , JPA , .

​​ HHH-5732. , , , .

Hibernate 4.1 , :

position MenuItem, getters seters. Hibernate :

public class MenuItem {

     public MenuItem(... other parameters ..., int position) {

     }

     @Column("position")
     private int position;

     .... no getters or setters ...

}

, :

int counter = 0; // some counter keeping the current position being added
MenuItem newItem = service.persist(.., counter); 
parent.getChildren().add(newItem);

, , .

: . Hibernate

+3

, @OrderColumn . JPA . Hibernate children , . . (non-mappedBy): setParent().

, , , ( MenuItem_MenuItem) . , , . . :

MenuItem newItem = new MenuItem();
newItem.setParent(parent);
entityManager.persist(newItem);

position ?

position MenuItem :

private Integer position;

@OrderColumn @OrderBy("position"):

@OneToMany(mappedBy = "parent", ...)
...
@OrderBy("position")
private List<MenuItem> children = new ArrayList<MenuItem>();

, :

public void reorder() {
    for (int i = 0; i < getChildren().size(); i++) {
        MenuItem menuItem = getChildren().get(i);
        menuItem.setMyOrder(i);
    }
}

@PrePersist @PreUpdate hooks . . / . , / .

+5

position , ( ), ? !

0

Hibernate mappedBy , :

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-collection-extratype-indexbidir

In your case, the children and parents are the same, so I'm interested to know how this will behave. Otherwise, the documentation describes the second option withoutmappedBy

0
source

All Articles