无法理解 EclipseLink 的警告信息

18
我正在使用EclipseLink 2.3.1和JPA 2来建模自引用表。当我创建EntityManager时,EclipseLink会出现奇怪的警告。
[EL Warning]: 2011-11-27 14:28:00.91--ServerSession(8573456)--Reverting the lazy setting on the OneToOne or ManyToOne attribute [redirectID] for the entity class [class lp.db.model.Site] since weaving was not enabled or did not occur.

我找不到任何关于这个警告的文档,也不确定它的含义。 我还想知道如何解决导致此警告出现的问题...

我对 JPA 还很陌生,所以可能是一个愚蠢的问题。 我的程序非常简单。以下是实体定义:

@Entity
@Table(name="site") 
public class Site implements Serializable {

private static final long serialVersionUID = 1L;

    @Id
    @Column(name="site_id")
    public String siteID;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="redirect_id", referencedColumnName="site_id")
    public Site redirectID;

    @Column(name="name")
    public String name;
}

这是 persistence.xml 文件:

<?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="lpdb2" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>lp.db.model.Site</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/lpdb2"/>
            <property name="javax.persistence.jdbc.user" value="blabla"/>
        </properties>
    </persistence-unit>
</persistence>

引起此警告的代码:

Persistence.createEntityManagerFactory("lpdb2").createEntityManager();

请注意,生成的EM可以正常使用(例如)查找元素。 此外,我可以遍历实体图 - 我可以在数据库中找到一个实体,然后使用redirectID字段获取另一个实体。

1个回答

20
请见http://wiki.eclipse.org/Introduction_to_EclipseLink_Application_Development_%28ELUG%29#Using_Weaving
为了实现在XxxToOne关联上的延迟加载,必须修改JPA实体的字节码(这就是织入的含义)。如果没有进行修改,则XxxToOne关联只能使用急切加载。
急切加载意味着每次从数据库加载Site时,它的redirectID也会被加载。使用延迟加载,你加载一个网站时,只有当你调用redirectID字段上的方法时,其重定向才会被加载(懒加载)。

谢谢!由于某些原因,我跳过了这个资源。我会去查一下的。 - gamliela
3
为了使动态织入工作,我不得不在JVM命令行中添加-javaagent:eclipselink.jar。请参阅http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Dynamic_Weaving。 - Nathan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接