JPA EntityManager find方法返回代理 [英] JPA EntityManager find method returns proxy

查看:974
本文介绍了JPA EntityManager find方法返回代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体。


$ b 示例:

  @Entity 
@Table(name =vehicle)
@Inheritance(strategy = InheritanceType.JOINED)
public class VehicleEntity {
// id等等,所有引用获取类型都是LAZY

$ b $实体
@Table(name =car)
public class CarEntity extends VehicleEntity {
//特殊参数,所有引用获取类型是LAZY
}

当我在一个现有汽车的EntityManager上调用一个find像这样:

  VehicleEntity vehicleEntity = entityManager.find(VehicleEntity.class,carID); 

我找回代理对象,但我需要访问CarEntity类的特殊方法,因为我想设置一些新的参数。



有人可以帮我吗我该怎么做?

当然,这只是一个示例问题。更具体地说:在我调用find之后,我正在检查返回对象的实例,如果它是CarEntity,我设置参数,如果没有,我什么都不做。我在CarEntity旁边有更多的继承类,并且像以前一样执行相同的过程。我知道我可以通过在特定对象类上查找来解决这个问题,但是我必须多次调用查找,并且当我在寻找任何VehicleEntity时,我总是对实际对象感兴趣最好的解决方案就是全球解决方案。

解决方案

我的理解是,因为您正在获取代理对象,类型VehicleEntity,并且该代理对象中的目标对象应该是CarEntity。



因此,在我看来,您可以对代理对象进行deproxy,这样您就可以获得真实该对象将是CarEntity。



然后,您可以拨打CarEntity的特殊会员。



我找到了链接,代理对象的通用方式(因为您有很多继承类)

将Hibernate代理转换为真实对象



希望它有帮助..


I have two entities. One inherited from the other.

Example:

@Entity
@Table(name = "vehicle")
@Inheritance(strategy = InheritanceType.JOINED)
public class VehicleEntity {
    //id, etc., all reference fetch type is LAZY
}

@Entity
@Table(name = "car")
public class CarEntity extends VehicleEntity {
    //special parameters, all reference fetch type is LAZY
}

When I call a find() on EntityManager with an existing car's id like this:

VehicleEntity vehicleEntity = entityManager.find(VehicleEntity.class, carID);

I get back a proxy object, but I need to access the special methods of the CarEntity class, because I would like to set some new parameters.

Can someone help me how can I do this?

Of course this is just an example problem. More specifically: after I call find I'm checking the instance of the returned object, if it is "CarEntity" I set the parameters, if not I don't do anything. I have much more inherited classes beside "CarEntity", and I do the same procedure like before. I know I could solve this with "find"s on the specific object classes, but then I have to call "find" several times, and when I'm looking for any "VehicleEntity" I'm always interested in the actual object so the best would be a global solution.

解决方案

What i understood is, since you are getting the proxy object, which should be of type VehicleEntity, and target object in that proxy object should be CarEntity.

So, in my opinion, you could deproxy the proxy object, so you could get the hold of real object which would be CarEntity.

Then, you could call the special member of CarEntity.

I have found the link, which would deproxy the proxy object in generic manner (since you had a lot of inherited classes)

Converting Hibernate proxy to real object

Hope it help..

这篇关于JPA EntityManager find方法返回代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆