2

I receive the following warning on the GlassFish terminal while deploying an application that has lazy fetching in entities,

WARNING: Reverting the lazy setting on the OneToOne or ManyToOne attribute [zoneTable] for the entity class [class entity.ZoneCharge] since weaving was not enabled or did not occur.

for whatever entities are listed in the project.

My persistence.xml file looks something like the following.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 
             xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence"
             http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="Project-ejbPU" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/pool</jta-data-source>

        <class>entity.ZoneTable</class>
        <!--Other classes-->
        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
            <property name="eclipselink.weaving" value="static"/>
            <property name="eclipselink.target-server" value="SunAS9"/>
            <property name="eclipselink.logging.parameters" value="true"/>
            <property name="eclipselink.logging.level.sql" value="FINEST"/>
            <property name="eclipselink.logging.level" value="FINEST" />
            <property name="eclipselink.logging.level" value="WARNING"/>
            <property name="eclipselink.logging.level.cache" value="FINEST"/>
        </properties>
    </persistence-unit>
</persistence>

The ZoneTable class looks like,

@Entity
@Table(name = "zone_table", catalog = "projectdb", schema = "")
public class ZoneTable implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "zone_id")
    private Long zoneId;

    @Column(name = "zone_name")
    private String zoneName;

    @OneToMany(mappedBy = "zoneTable", fetch = FetchType.LAZY)
    private Set<ZoneCharge> zoneChargeSet;

    //Setters and getters.
}

Here lazy fetching works even with remote EJBs but I think, it is considered eager because of the above warning.

How to enable weaving (or some other way) to avoid this warning?

When I enter this command as mentioned here,

java org.eclipse.persistence.tools.weaving.jpa.StaticWeave

on the command line, I get the following error.

Error: could not find or load main classorg.eclipse.persistence.tools.weaving.jpa.StaticWeave

What are the ways to overcome this warning?

Tiny
  • 27,221
  • 105
  • 339
  • 599

1 Answers1

1

Make sure you have the EclipseLink.jar with the org.eclipse.persistence.tools.weaving.jpa.StaticWeave on the classpath when trying to run that class for static weaving.

But if you are running in GlassFish 4 you should not need to use static weaving. Remove the <property name="eclipselink.weaving" value="static"/> property and try deploying again. This property indicates that your classes were already statically enhanced so that dynamic weaving should not occur. Since they weren't enhanced, lazy 1:1 and M:1 will default to eager.

Tiny
  • 27,221
  • 105
  • 339
  • 599
Chris
  • 20,138
  • 2
  • 29
  • 43
  • If that property is removed then, it throws this exception - `javax.servlet.ServletException: Exception Description: An attempt was made to traverse a relationship using indirection that had a null Session. This often occurs when an entity with an uninstantiated LAZY relationship is serialized and that lazy relationship is traversed after serialization. To avoid this issue, instantiate the LAZY relationship prior to serialization.` So, it is lazy without this property. – Tiny Sep 13 '13 at 15:54
  • That means weaving was successfully used, but you are traversing an unfetched lazy relationship after serializing the entity which isn't allowed. After serialization, the context isn't available, so the lazy relationship cannot be fetched. You must access the relationship prior to serialization (for instance by using join fetch over the relationship) or make the relationship eager so that it is fetched already. – Chris Sep 13 '13 at 17:13
  • Join fetch worked though it looks a bit ugly with the criteria API. Are relationships in this case, really not considered eager? – Tiny Sep 13 '13 at 19:09
  • Yes. Lazy lets the provider retrieve the referenced object when it needs to, which is when you access the relationship. Specifying fetch join on the relationship forces it to be fetched right away - eagerly. It shouldn't be too ugly, you just use the fetch api like you would a join. – Chris Sep 16 '13 at 13:37
  • The term "*weaving*" often sounds too technical to me. What does actually it mean in layman language, when someone mentions the "*weaving*" while using an ORM framework like "*You need to enable (static or dynamic) weaving*"? – Tiny Nov 22 '14 at 14:43
  • 1
    Byte code enhancement. It allows the provider to change your class to use it's constructs (EclipseLink's valueholders) to wrap your references as well as include other performance enhancements. It is neccessary for lazy 1:1, but not for lazy collections, as Eclipselink can use its own collection implementation directly. See https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving – Chris Nov 24 '14 at 14:50