In a department class, it has a parent department and multiple sub-departments. I hope that the entity that JPA queries from the database contains the parent department information and sub-department information, but the parent department information does not need to contain the information of its sub-department, and the sub-department collection does not need to contain the parent department information.
I am trying to add annotations on the parent department genus, but it does not work. The entity type returned by JPA is HibernateProxy, i am trying add HibernateProxyTypeAdapter to gson and add this code
new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY)
finaly, i am get result from postman is {}, yes, an empty object. this is not what i want.
this is department class:
@Setter
@Getter
@Entity
@Table(name = "department")
public class Department implements Serializable {
private static final long serialVersionUID = -3360650322926476819L;
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "SnowFlakeIdGenerator")
@GenericGenerator(name = "SnowFlakeIdGenerator", strategy = "cn.lmt.id.SnowFlakeIdGenerator")
private Long id;
@Column(name = "create_time", updatable = false)
private LocalDateTime createTime;
@Column(name = "update_time", insertable = false)
private LocalDateTime updateTime;
@PrePersist
private void onPersist() {
this.setCreateTime(LocalDateTime.now());
}
@PreUpdate
private void onUpdate() {
this.setUpdateTime(LocalDateTime.now());
}
@Column(name = "dept_name", length = 16)
private String deptName;
@ManyToOne
@JoinColumn(name = "parent_id")
private Department parent;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
private List<Department> children;
}
public class HibernateProxyTypeAdapter extends TypeAdapter<HibernateProxy> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return (HibernateProxy.class.isAssignableFrom(type.getRawType()) ? (TypeAdapter<T>) new HibernateProxyTypeAdapter(gson) : null);
}
};
private final Gson context;
private HibernateProxyTypeAdapter(Gson context) {
this.context = context;
}
@Override
public HibernateProxy read(JsonReader in) throws IOException {
throw new UnsupportedOperationException("Not supported");
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void write(JsonWriter out, HibernateProxy value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
// Retrieve the original (not proxy) class
Class<?> baseType = Hibernate.getClass(value);
// Get the TypeAdapter of the original class, to delegate the serialization
TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType));
// Get a filled instance of the original class
Object unproxiedValue = ((HibernateProxy) value).getHibernateLazyInitializer()
.getImplementation();
// Serialize the value
delegate.write(out, unproxiedValue);
}
}
I want parent and children information, but I don't want partment.chidren and children.parent information.