Hibernate 访问字段时出错,映射问题
Hibernate Error accessing field, mapping issues
我正在尝试实现休眠,并且正在查看我的一个使用注释的旧项目。我想要一个 manytoone 映射。这是 classes.
Class 用于保存 ListItems,保留对我现在尝试映射的其中所有项目的引用。
见private List<ListItem> listItems
@Entity
@Table(name = "ITEMLIST")
public class ItemList {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private UUID listId;
@Column(unique = true)
private String name;
@Enumerated(EnumType.STRING)
private ListTypes listType;
@OneToMany(mappedBy = "itemList")
private List<ListItem> listItems;
@Column(unique = true)
private int position;
ListItem class 保留对其当前所在列表的引用。
见private ItemList itemList;
@Entity
@Inheritance
@DiscriminatorColumn(name = "ITEMTYPE")
@Table(name = "LISTITEM")
public abstract class ListItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private UUID itemId;
@ManyToOne
@JoinColumn
private ItemList itemList;
private String url;
private String name;
private Date dateAdded;
private int position;
用于测试持久性的测试代码:
public ItemList createList(){
Session session = factory.openSession();
ItemList itemList = new ItemList();
session.save(itemList);
factory.close();
return itemList;
}
这给了我这个错误日志:
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.util.List<xxx.listorganizer.listorganizer.Interfaces.ListItem> xxx.listorganizer.listorganizer.Models.ItemList.listItems] by reflection for persistent property [xxx.listorganizer.listorganizer.Models.ItemList#listItems] : xxx.listorganizer.listorganizer.Models.ItemList@7955d081
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:75)
at org.hibernate.property.access.spi.GetterFieldImpl.getForInsert(GetterFieldImpl.java:90)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValuesToInsert(AbstractEntityTuplizer.java:570)
at org.hibernate.tuple.entity.PojoEntityTuplizer.getPropertyValuesToInsert(PojoEntityTuplizer.java:226)
at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert(AbstractEntityPersister.java:5170)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:271)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:198)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:128)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:193)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:178)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:75)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:108)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:618)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:611)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:606)
at xxx.listorganizer.listorganizer.DAOs.ListDAO.createList(ListDAO.java:21)
at xxx.listorganizer.listorganizer.ListorganizerApplication.main(ListorganizerApplication.java:14)
... 5 more
Caused by: java.lang.IllegalArgumentException: Can not set java.util.List field xxx.listorganizer.listorganizer.Models.ItemList.listItems to xxx.listorganizer.listorganizer.Models.ItemList
at ...
希望有人能帮忙:)
我认为它无法创建您的抽象对象 class
原来是我用UUID生成id的问题。将其更改为长期修复它。
我正在尝试实现休眠,并且正在查看我的一个使用注释的旧项目。我想要一个 manytoone 映射。这是 classes.
Class 用于保存 ListItems,保留对我现在尝试映射的其中所有项目的引用。
见private List<ListItem> listItems
@Entity
@Table(name = "ITEMLIST")
public class ItemList {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private UUID listId;
@Column(unique = true)
private String name;
@Enumerated(EnumType.STRING)
private ListTypes listType;
@OneToMany(mappedBy = "itemList")
private List<ListItem> listItems;
@Column(unique = true)
private int position;
ListItem class 保留对其当前所在列表的引用。
见private ItemList itemList;
@Entity
@Inheritance
@DiscriminatorColumn(name = "ITEMTYPE")
@Table(name = "LISTITEM")
public abstract class ListItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private UUID itemId;
@ManyToOne
@JoinColumn
private ItemList itemList;
private String url;
private String name;
private Date dateAdded;
private int position;
用于测试持久性的测试代码:
public ItemList createList(){
Session session = factory.openSession();
ItemList itemList = new ItemList();
session.save(itemList);
factory.close();
return itemList;
}
这给了我这个错误日志:
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.util.List<xxx.listorganizer.listorganizer.Interfaces.ListItem> xxx.listorganizer.listorganizer.Models.ItemList.listItems] by reflection for persistent property [xxx.listorganizer.listorganizer.Models.ItemList#listItems] : xxx.listorganizer.listorganizer.Models.ItemList@7955d081
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:75)
at org.hibernate.property.access.spi.GetterFieldImpl.getForInsert(GetterFieldImpl.java:90)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValuesToInsert(AbstractEntityTuplizer.java:570)
at org.hibernate.tuple.entity.PojoEntityTuplizer.getPropertyValuesToInsert(PojoEntityTuplizer.java:226)
at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert(AbstractEntityPersister.java:5170)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:271)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:198)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:128)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:193)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:178)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:75)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:108)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:618)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:611)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:606)
at xxx.listorganizer.listorganizer.DAOs.ListDAO.createList(ListDAO.java:21)
at xxx.listorganizer.listorganizer.ListorganizerApplication.main(ListorganizerApplication.java:14)
... 5 more
Caused by: java.lang.IllegalArgumentException: Can not set java.util.List field xxx.listorganizer.listorganizer.Models.ItemList.listItems to xxx.listorganizer.listorganizer.Models.ItemList
at ...
希望有人能帮忙:)
我认为它无法创建您的抽象对象 class
原来是我用UUID生成id的问题。将其更改为长期修复它。