0

我在 Spring 3.1.1 Hibernate 4.1.1 jsf 2.1.6 上有一个演示项目

***请忽略标签中“<”开始后的任何空格:***

在persistance.xml 文件中,我使用如下配置:


< ?xml version="1.0" encoding="UTF-8"?>
< persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

    <persistence-unit name="app-persistance"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <!-- ********* HAS Entries ********* -->
                <class>com.myapp.domain.classA</class>
                <class>com.myapp.domain.classB</class>
                <!-- ******More Domain classes here ***-->
< /persistence-unit>

< /persistence>

我的 applicationContext.xml 快照如下:


< bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" scope="singleton">
        
        <property name="persistenceUnitName" value="app-persistance" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="javax.persistence.validation.mode">none</prop>
                <prop key="javax.persistence.sharedCache.mode">all</prop>
                <prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.impl.FSDirectoryProvider
                </prop>
                <prop key="hibernate.search.default.indexBase">c:/lucene/indexes</prop>
            </props>
        </property>
        
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
        scope="singleton">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

现在,每次创建任何域类时,我都必须在 persistence.xml 中添加一个条目,所有类都已正确注释。

域类的快照:

@Indexed
@Entity
@Table(name = "USER")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class User implements Serializable {
    @Id
    @DocumentId
    @Column(name = "ID", length = 20)
    @Field(index = Index.YES)
    private String id;
        ................
        .................
  }

应该做些什么,以便不需要创建任何新的域类,persistence.xml 标记条目,并且在 DB 中自动创建 SQL 表

因为我已经在 applicationContext.xml 中添加了以下内容

< prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto} < /prop>

< prop key="hibernate.show_sql">${hibernate.show_sql} < /prop>
4

1 回答 1

0

entityManagerFactory在bean 声明中包含以下属性

<property name="packagesToScan" value="com.myapp.domain"/>

并从 persistence.xml 中删除所有类声明

于 2013-05-31T11:06:09.943 回答