Hibernate: foreign key without an object class, only by id

I have a hierarchical entity that refers to it as a parent. I need to do the mapping only through identifiers, not through entity instances (the reason is too complicated to explain). So I defined the object like this:

class Item {

    @Id
    private String id;

    @ManyToOne(targetEntity = Item.class)
    @JoinColumn(name = "PARENT_ID", nullable = true)
    private String parentId;

}

It seems to be working fine. A foreign key constraint is created correctly in the database. But when I execute the following query:

SELECT i FROM Item i WHERE i.parentId = :parentId

I get this exception (interesting parts are in bold):

org.hibernate.PropertyAccessException: IllegalArgumentException occurred when getter was called from com.example.dom.Item.id  at org.hibernate.property.BasicPropertyAccessor $BasicGetter.get(BasicPropertyAccessor.java:192)   at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:346)   at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4746)   at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4465)   at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:243)    org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:293)    org.hibernate.type.EntityType.getIdentifier(EntityType.java:537)    org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:174)    org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)    org.hibernate.loader.hql.QueryLoader.bindParameterValues ​​(QueryLoader.java:616)    org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1901)    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839)    org.hibernate.loader.Loader.doQuery(Loader.java:910)    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)    org.hibernate.loader.Loader.doList(Loader.java:2554)    org.hibernate.loader.Loader.doList(Loader.java:2540)    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)    org.hibernate.loader.Loader.list(Loader.java:2365)    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:497)    org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)    org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:236)   at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1300)   at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)    com.example.dao.ItemDao.findChildrenByParentId(ItemDao.java:43)   at com.example.dao.ItemDao $$ FastClassBySpringCGLIB $$ 51b04ce9.invoke()    org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)    org.springframework.aop.framework.CglibAopProxy $CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)   at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)   ... 47 : java.lang.IllegalArgumentException:   at sun.reflect.NativeMethodAccessorImpl.invoke0 ( )   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)    java.lang.reflect.Method.invoke(Method.java:597)   at org.hibernate.property.BasicPropertyAccessor $BasicGetter.get(BasicPropertyAccessor.java:169)   ... 76

, Hibernate parentId, Item, String.

?

, , . ( , ).

+4
3

( Item). , , , , .

0

id, , .

@Column(name="id")
0

, .

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private String id;


@ManyToOne
@JoinColumn(name = "parent_id",nullable = true)
private Item item;
0

Source: https://habr.com/ru/post/1608551/


All Articles