EJB Glassfish v3.1.2 client passed data to session

2019-05-13 19:18发布

I am having a problem when calling session bean method passing method parameters from client

application, the data reaches the method call is always null or set to default value.

while the process of the method works well with the object

for example:

-we have method to persist an object entity addStudent(Student student); - from the client we create the student object setting student fields like student name and so on, calling the method addStudent(ourStudent); this ourStudent reaches method with fields of null or default value. the student is added with these empty fields.

Thanks in advance.

1条回答
爷、活的狠高调
2楼-- · 2019-05-13 20:04

You are using EclipseLink with weaving, and it doesn't work. You should try without weaving. Probably by editing your persistence.xml(s)

<persistence-unit name="XXX" transaction-type="XXX">
    <jta-data-source>XXX</jta-data-source>
    <jar-file>Or List of Classes or something else</jar-file>
    <properties>
      [other properties]
      <property name="eclipselink.weaving" value="false"/>
    </properties>
  </persistence-unit>

Update: There are several alternative ways a JPA implementation could handle entities, this is a none exhausting list:

  • Extension (this is way the JPA spec requires a none private default constuctor for entities)
  • Wrapping
  • Byte code manipulation of the class (to make it conform to how EclipseLink "wants" it to be)
  • ThreadLocal proxy thingie
  • Basic reflection using the properties
  • Basic reflection using the getters setters (if there are any)

EclipseLink calls byte code injection "Weaving" ( What is Java bytecode injection? ) Dynamic weaving is doing the weaving at "runtime" - basically when the class is loaded by a class loader. Static weaving is doing the weaving before deployment, but after compilation. For EclipseLink weaving is the fastest method performance wise, it is also the prefered method for other reasons. Unfortuneatly it is often a bit tricky to get weaving to work. It is fully possible none of that matters for your project, it doesn't for a lot of typical projects.

If there are clients that access beans via an remote interface, and there are entities passed as arguments or returns value through that connection dynamic weaving won't work. In most production scenarios, especially if the app/product isn't very small static weaving is prefered over dynamic weaving anyways ... To read more about static vs dynamic weaving and how to configure it I haven't really found any excellent sources, but this one is at least semi official: Using_EclipseLink_JPA_Weaving

What was happening to you was that the entity was weaved at one end and not weaved at the other -> can absolutely not work.

The good news is that you probably don't have to care about any of this weaving thing at all, or you might. When you disabled weaving, EclipseLink fell back to another method for handling the JPA entities. There are some functions EclipseLink only supports if weaving is enabled (none JPA required though).

From: What_You_May_Need_to_Know_About_Weaving_JPA_Entities Comes a list of things that EclipseLink explicitly uses weaving for:

  • lazy loading (indirection)
  • change tracking
  • fetch groups
  • internal optimizations

(For some of them there are fallbacks to other methods if weaving is disabled, I'd guess all but "internal optimizations")

查看更多
登录 后发表回答