I'm developing an application using java with hibernate 4.2.6 and spring 4.0.1. My application is a REST FULL
application. for this I use jackson. My entities are as follow:
Calk.java:
@Entity
public class Calk {
@Id
@GeneratedValue
private Long id;
@OneToMany(mappedBy="calk", fetch=FetchType.LAZY)
List<BaseLayer> baseLayer = new ArrayList<BaseLayer>();
public void addBaseLayer(BaseLayer baseLayer){
this.baseLayer.add(baseLayer);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonIgnore
public List<BaseLayer> getBaseLayer() {
return baseLayer;
}
public void setBaseLayer(List<BaseLayer> baseLayer) {
this.baseLayer = baseLayer;
}
}
BaseLayer.java:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="layer")
@JsonSubTypes(
{
@JsonSubTypes.Type(value=PointLayer.class, name="point")
})
@DiscriminatorValue("BaseLayerDiscriminator")
public class BaseLayer {
@Id
@GeneratedValue
protected Long gid;
@ManyToOne(fetch = FetchType.LAZY)
protected Calk calk;
public Long getGid(){
return gid;
}
public void setGid(Long gid){
this.gid = gid;
}
@JsonIgnore
public Calk getCalk(){
return calk;
}
public void setCalk(Calk calk){
this.calk = calk;
}
}
Now I have a class that extends from BaseLayer.java
as follow:
PointLayer.java:
@Entity
@DiscriminatorValue("PointDiscriminator")
public class PointLayer extends BaseLayer{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Now I create a json as follow and then send it to a controller:
{"layer": "point", "calk":{"id":1}, "name": "itsme"}
Now the controller defines as follow:
@RequestMapping("\test")
public String test(@RequestBody BaseLayer baseLayer){
System.out.println(baseLayer.getName());// this print "itsme"
Calk calk = baseLayer.getCalk();//it return null
if(calk == null)
return "its null";
else
return "its not null";
}
when we call the controller it return its not null
. The calk
should not be null
.
Where is the problem?
Update:
When I remove @JsonIgnore
at getCalk
, It work fine. But Why? I want to ignore getCalk
but NOT ignore setCalk
.