无法从实体类Hibernate 4.2.7 Websphere 8.5.5 oracle 11g创建表

时间:2015-06-25 16:36:23

标签: java hibernate jpa oracle11g

我自动从@Entity类创建表时遇到问题。当我手动创建表时一切正常。我可以这样注入entityManager。我找到了许多像http://docs.mongodb.org/manual/reference/operator/query/and这样的解决方案,但对我来说并不起作用。我的配置: persistence.xml

    <persistence-unit name="entityManagerFactory"
    transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jdbc/oracleTest</jta-data-source>
    <class>pl.example.was.test.entity.ResourceEntity</class>

    <properties>
        <property name="packagesToScan" value="pl.example.was.test.entity" />
        <property name="hibernate.archive.autodetection" value="class, hbm" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        <property name="show_sql" value="true" />
        <property name="format_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="true" />

        <property name="hibernate.transaction.factory_class"
            value="org.hibernate.transaction.CMTTransactionFactory" />
        <property name="hibernate.transaction.manager_lookup_class"
            value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
    </properties>
</persistence-unit>

实体类的一部分

@Entity
 public class ResourceEntity implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String name;
// getters setters //

注入EntityManager

@PersistenceContext
private EntityManager em;

我正在创建两个测试方法

public void addResource(ResourceEntity selectedResource, Locale locale) {
    selectedResource.setLocale(locale.toString());
    try{
    Session session = em.unwrap(Session.class);
    Transaction tx = session.beginTransaction();
    session.save(selectedResource);
    session.flush();
    session.clear();
    tx.commit();
    session.close();
    }catch (Exception ex){
        ex.printStackTrace();
    }
}

public void addResource2(ResourceEntity selectedResource, Locale locale) {
    selectedResource.setLocale(locale.toString());
    em.persist(selectedResource);
}

我正在阅读,我必须在表格自动创建后创建Session对象或EntityManager,但不是在我的情况下。 我的hibernate dependecies

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.2.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.7.Final</version>
    </dependency>

有人有任何想法吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

这个肯定有问题:

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

您要求Hibernate在会话结束时删除架构。这就是为什么你最终没有桌子。你可能想要的是:

<property name="hibernate.hbm2ddl.auto" value="create" />

此外,true不是hibernate.hbm2ddl.auto的有效值,所以最好删除它:

<property name="hibernate.hbm2ddl.auto" value="true" />

这已经讨论过here