所以我读了这个主题: deserialize lazy loading in hibernate and jackson
我希望与之前的作者达到相似或实际相同的效果。但是现在两天我被困住了,我不理解我的错误。
所以f.e我有Hibernate实体的UserModel和AuthoritiesModel类:
的usermodel:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "username")
@JsonAutoDetect
@Entity
@Table(name = "users")
public class UserModel implements Serializable, Cloneable {
private static final long serialVersionUID = 8019466778684575940L;
@Id
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "enabled")
private Boolean enabled;
@Column(name = "email")
private String email;
@Column(name = "name")
private String name;
@Version
@Column(name = "VERSION")
private int version;
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, targetEntity = AuthorityModel.class)
private List<AuthorityModel> authorities = new ArrayList<AuthorityModel>();
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, targetEntity = MessageModel.class)
private List<MessageModel> messages = new ArrayList<MessageModel>();
@ManyToOne(fetch = FetchType.LAZY, targetEntity = StatusModel.class)
@JoinColumn(name = "status")
private StatusModel status;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "meeting_user", joinColumns = { @JoinColumn(name = "username", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "id_meeting", nullable = false, updatable = false) })
private List<MeetingModel> meetings = new ArrayList<MeetingModel>();
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "contact_user", joinColumns = @JoinColumn(name = "username", nullable = false, updatable = false), inverseJoinColumns = @JoinColumn(name = "contact_username", nullable = false, updatable = false))
private List<UserModel> contacts = new ArrayList<UserModel>();
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "contact_user", joinColumns = @JoinColumn(name = "contact_username", nullable = false, updatable = false), inverseJoinColumns = @JoinColumn(name = "username", nullable = false, updatable = false))
private List<UserModel> contactOf = new ArrayList<UserModel>();
AuthorityModel:
@JsonAutoDetect
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Entity
@Table(name = "authorities")
public class AuthorityModel extends BaseEntity implements GrantedAuthority {
private static final long serialVersionUID = -3950709468104459415L;
public static final String ROLE_USER = "ROLE_USER";
public static final String ROLE_ADMIN = "ROLE_ADMIN";
@Column(name = "authority")
private String authority;
@ManyToOne(fetch = FetchType.LAZY, targetEntity = UserModel.class)
@JoinColumn(name = "username")
private UserModel user;
我正在使用Hibernate4Module
来管理实体,但如果我离开h.configure(Feature.FORCE_LAZY_LOADING, false);
,我最终会将所有集合清空(null),如:
{
"username":"rod",
"password":"e97673c55fc4f2af8bf2122333df24ac",
"enabled":true,
"email":null,
"name":null,
"version":1,
"authorities":null,
"messages":null,
"status":null,
"meetings":null,
"contacts":null,
"contactOf":null
}
如果我将其设为Eager或h.configure(Feature.FORCE_LAZY_LOADING, true);
我将加载整个数据库,如下所示:
{
"username":"rod",
"password":"e97673c55fc4f2af8bf2122333df24ac",
"enabled":true,
"email":null,
"name":null,
"version":1,
"authorities":[
{
"id":"1",
"version":0,
"description":null,
"comment":null,
"authority":"ROLE_USER",
"user":"rod"
}
],
"messages":[
{
"id":"1",
"version":0,
"description":null,
"comment":null,
"message":"TEST",
"user":"rod",
"meeting":{
"id":"1",
"version":0,
"description":null,
"comment":null,
"users":[
{
"username":"bob",
"password":"8ee3086749f7fa95ffe9c4588037cb10",
"enabled":true,
"email":null,
"name":null,
"version":1,
"authorities":[
{
"id":"2",
"version":0,
"description":null,
"comment":null,
"authority":"ROLE_ADMIN",
"user":"bob"
},
{
"id":"3",
"version":0,
"description":null,
"comment":null,
"authority":"ROLE_USER",
"user":"bob"
}
],
"messages":[
{
"id":"2",
"version":0,
"description":null,
"comment":null,
"message":"TEST BACK",
"user":"bob",
"meeting":"1"
}
],
"status":{
"id":"1",
"version":0,
"description":null,
"comment":null,
"status":"available",
"users":[
"bob",
"rod"
]
},
"meetings":[
"1"
],
"contacts":[
"rod"
],
"contactOf":[
"rod"
]
},
"rod"
],
"messages":[
"1",
"2"
]
}
}
],
"status":"1",
"meetings":[
"1"
],
"contacts":[
"bob"
],
"contactOf":[
"bob"
]
}
为什么要加载所有内容,而不仅仅是一个集合?我想只使用引用对象的ID或类似的东西来收集集合,但我找不到这样做的方法。
任何帮助表示赞赏!
---澄清 我期待类似的东西:
{
"username":"rod",
"password":"e97673c55fc4f2af8bf2122333df24ac",
"enabled":true,
"email":null,
"name":null,
"version":1,
"authorities":[
{
"id":"1",
}
],
"messages":[
{
"id":"1",
}
],
"status":"1",
"meetings":[
"1"
],
"contacts":[
"bob"
],
"contactOf":[
"bob"
]
}
答案 0 :(得分:1)
只需从fetch = FetchType.LAZY
注释中移除@JoinColumn
以获取您希望加载的属性,并将其保留为您不想获取的属性(应为Collection
类型)