休眠默认架构不起作用 [英] Hibernate Default Schema not working

查看:114
本文介绍了休眠默认架构不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <持久性单位名称= 商店 > 

< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< jar-file> store-data-0.0.4-SNAPSHOT.jar< / jar-file>

<属性>
name =hibernate.show_sql
value =true/>
< property
name =hibernate.format_sql
value =true/>
name =hibernate.default_schema
value =Store>< / property>
name =hibernate.archive.autodetection
value =class/>
< / properties>
< / persistence-unit>

然而,当我运行一个简单的测试查询时:

  @PersistenceContext 
私有EntityManager管理器;

@Transactional
public List< Person> listPeople(){
return manager.createQuery(SELECT p FROM Person p,Person.class).getResultList();
}

我得到这个:

无效的对象名称'Person'。



运行本机查询时:

  manager.createNativeQuery(SELECT * FROM Store.Person,Person.class).getResultList(); 

一切正常。



m使用Spring运行SE应用程序。我之前在Web应用程序中使用了 hibernate.default_schema ,它工作得很好。



这就是我声明了实体映射:

 < bean 
id =entityManagerFactory
class =org .springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
name =dataSource
ref =mysqlDataSource/>
name =packagesToScan
value =com.base.package/> <! - 所有实体的基本包 - >
< property name =jpaVendorAdapter>
< bean
class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< / property>
< / bean>

有什么想法?

解决方案

 < / p>我可以通过在我的Spring上下文文件中直接设置默认模式来解决问题。 bean 
id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
name =dataSource
ref =mysqlDataSource/>
name =packagesToScan
value =com.base.package/> <! - 所有实体的基本包 - >
< property name =jpaVendorAdapter>
< bean
class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< / property>
< property name =jpaProperties>
<道具>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.format_sql> true< / prop>
< prop key =hibernate.default_schema> Store< / prop>
< /道具>
< / property>
< / bean>


I'm trying to set a default schema in my Hibernate mapping, like so:

<persistence-unit name="store">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jar-file>store-data-0.0.4-SNAPSHOT.jar</jar-file>

    <properties>
        <property
            name="hibernate.show_sql"
            value="true" />
        <property
            name="hibernate.format_sql"
            value="true" />
        <property
            name="hibernate.default_schema"
            value="Store"></property>
        <property
            name="hibernate.archive.autodetection"
            value="class" />
    </properties>
</persistence-unit>

However, when I run a simple test query:

@PersistenceContext
private EntityManager manager;

@Transactional
public List<Person> listPeople() {
    return manager.createQuery("SELECT p FROM Person p", Person.class).getResultList();
}

I get this:

Invalid object name 'Person'.

When I run a native query:

manager.createNativeQuery("SELECT * FROM Store.Person", Person.class).getResultList();

Everything works fine.

I'm running a SE application, using Spring. I have used the hibernate.default_schema in a Web App before, and it worked just fine.

This is how I declare the entities mapping:

<bean
    id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property
        name="dataSource"
        ref="mysqlDataSource" />
    <property
        name="packagesToScan"
        value="com.base.package" /> <!-- base package for all my entities -->
    <property name="jpaVendorAdapter">
        <bean
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
</bean>

Any thoughts?

解决方案

I could solve the problem by setting the default schema directly in my Spring context file:

<bean
    id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property
        name="dataSource"
        ref="mysqlDataSource" />
    <property
        name="packagesToScan"
        value="com.base.package" /> <!-- base package for all my entities -->
    <property name="jpaVendorAdapter">
        <bean
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.default_schema">Store</prop>
            </props>
        </property>
</bean>

这篇关于休眠默认架构不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆