我使用Jackson以json格式读取文件并将结果放入ormlite数据库中,但是当我读取对象时,我得到一个异常:
com.fasterxml.jackson.databind.JsonMappingException:属性“objectCache”的冲突setter定义:com.j256.ormlite.dao.Dao #setObjectCache(1 params)vs com.j256.ormlite.dao.Dao #setObjectCache( 1参数)
我有一个班级来宣布ormlite和杰克逊的职业:
@DatabaseTable
@JsonDeserialize
public final class Items extends BaseDaoEnabled<Items, String> implements Serializable{
@DatabaseField (canBeNull = false, id = true, width = 10)
private String item = "";
@DatabaseField (canBeNull = true, dataType = DataType.BIG_DECIMAL)
@JsonIgnore
private BigDecimal dto = BigDecimal.ZERO;
@JsonIgnore
@ForeignCollectionField (eager = false)
private ForeignCollection<Group> groups = null;
@DatabaseField (canBeNull = true, foreign = true)
@JsonIgnore
transient private Price price = null;
@JsonCreator
public Items(@JsonProperty("items") String items) {
this.items = items;
}
@JsonIgnore
public Items(String item) {
this.item = item;
}
}
添加Json文件是:[{“item”:“AAAAAA”},{“item”:“BBBBBB”}] 我尝试了不同的选项,如
@JsonIgnore //in class properties
ObjectMapper object = new ObjectMapper();
object.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
但两种选择都没有解决问题。有谁能够帮我?感谢。
答案 0 :(得分:0)
我创建了一个新类来解释Json(ItemsJson.class)并在JsonCreator中插入或更新元素,例如:
@JsonCreator
public ItemsJson(@JsonProperty("item") String item, @JsonProperty("groups") String groups) {
if(item != null){
try {
OpenHelper helper = OpenHelper.getHelper(getContext());
Dao<Items, String> daoItems = helper.getDao(Items.class);
Items itemAux = new Items(item);
daoItems.refresh(itemAux);
if(groups != null){
Dao<Groups, String> daoGroups = helper.getDao(Groups.class);
Groups groupsAux = new Groups(groups) ;
daoGroups.createIfNotExists(groupsAux);
daoGroups.refresh(groupsAux);
itemAux.setGroups(groupsAux);
}
daoItems.createOrUpdate(itemAux);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}