Java 休眠默认架构不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24846636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate Default Schema not working
提问by felipe_gdr
I'm trying to set a default schema in my Hibernate mapping, like so:
我正在尝试在我的 Hibernate 映射中设置默认架构,如下所示:
<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.
我正在使用 Spring 运行 SE 应用程序。我hibernate.default_schema
之前在 Web 应用程序中使用过,它工作得很好。
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?
有什么想法吗?
采纳答案by felipe_gdr
I could solve the problem by setting the default schema directly in my Spring context file:
我可以通过直接在我的 Spring 上下文文件中设置默认模式来解决这个问题:
<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>