我是Hibernate的新手并且遇到了问题。我无法保存包含List<的CustomObject。列表与LT;坐标>> 。我正在使用SpingBoot的MySQL。
CustomOjbect:
@Entity
public class CustomOjbect implements Serializable{
private static final long serialVersionUID = -4288223602669270676L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Column(name = "img")
private String img;
@OneToOne(cascade=CascadeType.ALL)
private Feet feet;
@OneToMany(cascade=CascadeType.ALL, targetEntity=Coordinate.class)
private List<List<Coordinate>> coordinates = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
//Other getter and setters here...
}
坐标:
@Entity
public class Coordinate implements Serializable{
private static final long serialVersionUID = 7288778461661904967L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Column(name = "y")
private Float y;
@Column(name = "x")
private Float x;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
//Getter and setters here...
}
脚:
@Entity
public class Feet implements Serializable {
private static final long serialVersionUID = -8438970667123556624L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Column(name = "height")
private Integer height;
@Column(name = "width")
private Integer width;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
//Getter and setters here
}
当我做List&lt;坐标&GT;而不是List&lt; &LT;列表与LT;坐标&gt;&gt;,但我必须更改它,因为从客户端发送的对象更改为对象数组的数组。
我该如何解决这个问题?
错误:
java.lang.IllegalArgumentException:无法设置java.lang.Integer 字段com.XXXX.XXXX.XXXX.Coordinate.id到 java.util.ArrayList at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(未知 来源)〜[na:1.8.0_111] at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(未知 来源)〜[na:1.8.0_111] at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(未知来源) 〜[na:1.8.0_111] at sun.reflect.UnsafeObjectFieldAccessorImpl.get(未知来源) 〜[na:1.8.0_111] at java.lang.reflect.Field.get(Unknown Source) 〜[na:1.8.0_111] at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:39) 〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:225) 〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4675) 〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4376) 〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226) 〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:510) 〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:99) 〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:765) 〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:758) 〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.jpa.event.internal.core.JpaPersistEventListener $ 1.cascade(JpaPersistEventListener.java:80) 〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:398) 〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323) 〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:162) 〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:431) 〜[冬眠核-5.0.12.Final.jar:5.0.12.Final]
在控制器中:
@RequestMapping(method = RequestMethod.POST, value = "/saveCustomObjects", consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String saveImageDetails(@RequestBody CustomObject customObject) {
customObjectRepository.save(customObject);
return "Saved successfully";
}
修改:
请问我是否需要了解任何内容。
答案 0 :(得分:0)
对于那些面临这个问题的人。
正如MGorgon在实体List
的评论List< Object>
中指出的那样,有效的jpa关系而List
的{{1}} List
} 不是有效的jpa关系。
为了解决这个问题,我改变了从客户端发送的json格式。
之前的JSON格式:
List< List< Object>>
更改后的JSON格式:
{
"coordinates": [
[
{
},
{
}
[
{
},
{
},
{
},
{
}
]
]
}
注意:我已经删除了json中的一些属性以减少代码。
希望它会对某人有所帮助。