I have the following mapping in my User DTO to store a Map
of user properties:
@Entity(name = "User")
public class User{
@Id
@Column(name = "Id")
private long userId;
@OneToMany
@JoinColumn(name = "properties")
private Map<Long, Property> properties= new LinkedHashMap<Long, LkpAIT>();
}
@Entity(name = "Property")
public class Property{
@Id
@Column(name = "Id")
private long propertyId;
@Column(name = "Name")
private String propertyName;
}
And the following @Controller method
@RequestMapping(value = "/getUser", produces = "application/json", method = RequestMethod.GET)
public @ResponseBody Page<LkpFeed> getLookupFeeds(Model model) {
page = 1;
Page<User> users = userRepo.getUsers(new PageRequest(page, 100)); //returns 100 users
return feeds;
}
The @Controller method executes fine (all users are found) however when the return statement executes, I get the following error:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.myapp.dto.Properties.properties, could not initialize proxy - no Session
org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:155)
org.hibernate.collection.internal.PersistentMap.isEmpty(PersistentMap.java:145)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:399)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:27)
com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:505)
com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:639)
com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:100)
Based on the exception my properties field is what's causing the error. I've experimented with fetchTypes
and PersistenceContexts
but am currently at a loss. Any ideas?
You can do several things
add fetch Eager in @OneToMany
@Entity(name = "User") public class User{ @Id @Column(name = "Id") private long userId; @OneToMany(fetch=FetchType.EAGER) @JoinColumn(name = "properties") private Map<Long, Property> properties= new LinkedHashMap<Long, LkpAIT>(); }
Or if you don't want the Properties in your returned JSON when you want to list only the users. You can add a @JsonIgnore
for properties
@Entity(name = "User") public class User{ @Id @Column(name = "Id") private long userId; @JsonIgnore @OneToMany @JoinColumn(name = "properties") private Map<Long, Property> properties= new LinkedHashMap<Long, LkpAIT>(); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.