JPA @OneToMany and @ManyToOne: backward link is zero

I have the following data structure.

@Entity
public class Device extends AbstractEntity implements Serializable{
    private int id;
    //...
    private List<Item> items;

    @OneToMany(fetch=FetchType.EAGER) 
    public List<Item> getItems() {
 return configurationItems;
    }
}

each element contains a link back to the device:

class Item {
    private Device;
 @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH} )
 public Device getDevice() {
  return device;
 }
}

I can create a device, add items and save it all. I can get objects from the database, and everything works, except for a link to the device that is stored in the element.

And it doesn’t matter how I read the elements: 1. read the device with all the related elements 2. read the objects

The device reference is always zero. I think something is wrong with my @ManyToOne annotation.

I use hibernate and spring, implementing DAO by subclassing HibernateDaoSupport.

Here is an example of code that retrieves all elements:

getHibernateTemplate().loadAll(Item.class)
+5
source share
1

relathionship, mappedBy:

@OneToMany(fetch=FetchType.EAGER, mappedBy = "device")  
public List<Item> getItems() { 
    return configurationItems; 
} 

. :

+7

All Articles