Getting Javassist types instead of actual Hibernate entity types

I came across a really unpleasant situation: I use Hibernate and Spring as a backend for my application, and it seems that in some cases entities that are in a relationship with a specific object are not obtained as regular objects objects from the database, but as types of javassists. For instance:.

I have a Campaign object with the following relationships:

@Entity @Table(name = "campaign") public class Campaign implements Serializable { [..] @ManyToMany(fetch = FetchType.LAZY) @JoinTable(uniqueConstraints = @UniqueConstraint(columnNames = { "campaign_id", "dealer_id" }), name = "campaign_has_dealer", joinColumns = { @JoinColumn(name = "campaign_id", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "dealer_id", nullable = false) }) private List<Dealer> dealers = new ArrayList<Dealer>(); @ManyToMany // (fetch = FetchType.LAZY) @JoinTable(uniqueConstraints = @UniqueConstraint(columnNames = { "campaign_id", "sales_area_id" }), name = "campaign_has_sales_area", joinColumns = { @JoinColumn(name = "campaign_id", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "sales_area_id", nullable = false) }) private List<SalesArea> salesAreas = new ArrayList<SalesArea>(); } 

When I receive sales information related to this campaign, I get a list of SalesArea _ $$ _ javassist_56, while for dealers I get regular Hibernate objects. Since the client side is based on GWT, we use RequestFactory to retrieve the material. Initially, I thought it was a problem with proxies, locators, etc., but I set a breakpoint in the service where they are retrieved, and they are Javassist objects immediately after they are selected. It seems that even deleting the FetchType.LAZY annotation (although this is certainly not the desired solution), the same thing happens. This happened with other types of relationships, not just @ManyToMany.

We use GWT 2.3, Spring 3, Hibernate 3.6.3, and JPA 2.0 for annotations.

Any suggestions would be appreciated.

Thanks in advance

+6
source share
3 answers

As far as I can see, the big problem you are facing is not so much the type of fetching of your association as the fact that proxied types do not work with RequestFactory.

Yes, this can be solved by changing the sampling strategy, but it sounds more like a weak workaround that could violate strange circumstances.

I don’t remember exactly how to solve it, but I did it, and as far as I remember, there was an extension point in the ServiceLayerDecorator class. You basically check if the object you are returning is a Hibernate proxy (check the Hibernate and HibernateProxy classes), and then return the non-proxy type instead to ServiceLayerDecorator. ( http://code.google.com/p/google-web-toolkit/issues/detail?id=6767 )

As for your receiving strategy, I would highly recommend @BatchSize (N), where N is large (maybe 1000), but it is an independent entity.

Good luck

+4
source

If you call the static method: HibernateProxyHelper.getClassWithoutInitializingProxy (legal entity); you get the proxy object class and the class itself if it is not proxied.

+1
source

With the Hibernate proxy model and now using its Javassist to avoid the slow traditional Hibernate runtime analysis, things will not be as elegant as the clean, intuitive experience of people who use complete solutions to improve bytecode, such as implementations JDO (e.g. DataNucleus).

Personally, I never see the point of saving (pardon) solutions that cause so many problems and populate the Internet with questions about broken code that requires strange, unintuitive workarounds, but still people do ...

However, back to the question: one solution to your problem, if you use JPA, is to use DataNucleus / JPA, which brings many advantages to DataNucleus / JDO (pure basic implementation - without proxies, without Javassist classes, etc.) in the implementation compatible with JPA, meaning you don’t need to change existing source code to start using it.

0
source

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


All Articles