Hibernate 4.3初始化实体

时间:2014-07-11 04:59:09

标签: java eclipse hibernate eclipse-kepler

我是Hibernate 4.3的新手,需要初始化实体的实例。 使用hibernate 3我使用了Hibernate.initialize(),但在版本4.3中,没有找到导入org.hibernate.Hibernate的命令。

我使用了Eclipse Kepler和Wildfly 8.0。

能告诉我怎么做吗?

由于

enter image description here enter image description here

2 个答案:

答案 0 :(得分:2)

问题仅在于 classpath ,因为您无法正确查看导入,我还建议您使用以下代码段初始化代理。

创建一个类 HibernateUtils 并将其称为静态方法。

public class HibernateUtils {

    public static <T> T initializeAndUnproxy (T entity) {
        if (entity == null) {
            return null;
        }

        if (entity instanceof HibernateProxy) {
            entity = (T) ((HibernateProxy) entity).getHibernateLazyInitializer().getImplementation();
        }

        return entity;
    }


}

答案 1 :(得分:0)

需要考虑的事项:

您是否在lib文件夹中包含了hibernate jar文件(特别是必需的jar文件)?

antlr-2.7.7.jar
commons-collections-3.2.1.jar
dom4j-1.6.1.jar
javassist-3.12.1.GA.jar
hibernate-core-4.0.1.Final.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
jboss-logging-3.1.0.CR2.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar

此文件来自hibernate您将下载,required内有lib个文件夹。

If yes

右键单击包含错误的导入,然后点击Fix project设置,系统会自动包含jar files