我刚接触spring / hibernate。我写了一个webservice,应该返回完整的对象,有时还会减少对象。例如,我有一个继承自Member
类的MemberBase
类:
...
**
* MemberBase
*/
@Entity
@Table(name = "MemberBase")
@Inheritance(strategy = InheritanceType.JOINED)
@SequenceGenerator(name = "idgen", sequenceName = "seq_Member")
public class MemberBase extends BasePojo {
@Column(name = "alias")
private String alias = null;
@Column(name = "firstName")
private String firstName = null;
@Column(name = "gender")
@Enumerated(EnumType.STRING)
private Gender gender = null;
@Transient
@OneToMany(fetch = FetchType.LAZY, mappedBy = "creator")
@ElementCollection(targetClass=OutingBase.class)
private Set<OutingBase> outings = null;
...
}
和成员:
...
/**
* Member
*/
@Entity
@Table(name = "Member")
@SequenceGenerator(name = "idgen", sequenceName = "seq_Member")
public class Member extends MemberBase {
@Enumerated(EnumType.STRING)
private MaritalStatus maritalStatus = null;
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
private LocalDate birthDate = null;
private String locationName = null;
private DateTime lastConnexion = null;
private String description = null;
private String videoMoodUrl = null;
private String mood = null;
private Boolean canCertify = null;
private String fid = null;
...
}
在我的控制器中,我使用hibernate获取成员库列表:
public ResponseEntity<List<MemberBase>> membersGet() {
Session session = HibernateUtils.getSessionFactory().openSession();
session.beginTransaction();
List<MemberBase> personList = session.createQuery("FROM " + MemberBase.class.getName(), MemberBase.class)
.getResultList();
session.close();
return new ResponseEntity<List<MemberBase>>(personList, HttpStatus.OK);
}
以下是我创建会话工厂的方法:
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration().configure();
configuration.addAnnotatedClass(BasePojo.class);
configuration.addAnnotatedClass(MemberBase.class);
configuration.addAnnotatedClass(Member.class);
configuration.addAnnotatedClass(OutingBase.class);
configuration.addAnnotatedClass(Outing.class);
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
return configuration.buildSessionFactory(builder.build());
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
我的问题是,当我致电membersGet()
时,我会检索List<Member>
而不是List<MemberBase>
的列表。没有崩溃,但我的序列化Json包含太多字段。我实现了这个继承,以避免获取所有Member
的字段。我做错了什么?
答案 0 :(得分:1)
我建议使用FasterXML Jackson的Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try (Realm realm = Realm.getDefaultInstance()) {
// No need to close the Realm instance manually
}
}
});
thread.start();
anotation。
如名称所示,在解析对象时,anotation将导致忽略该属性。
即G。
JsonIgnore
编辑:
使用@JsonIgnore
private String locationName = null;
指定应显示/解析属性的视图。
即G。
JsonView
当您映射它时,您可以指定所需的视图。
public class JsonView {
public static class Base {
}
public static class Extended extends Base {
}
}
@JsonView(JsonView.Extended.class)
private String locationName = null;
@JsonView(JsonView.Base.class)
private String alias = null;