Java 如何在 JPA 配置中设置默认模式名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2737420/
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
How to set up default schema name in JPA configuration?
提问by Roman
I found that in hibernate config file we could set up parameter hibernate.default_schema
:
我发现在休眠配置文件中我们可以设置参数hibernate.default_schema
:
<hibernate-configuration>
<session-factory>
...
<property name="hibernate.default_schema">myschema</property>
...
</session-factory>
</hibernate-configuration>
Now I'm using JPA and I want to do the same. Otherwise I have to add parameter schema
to each @Table annotation like:
现在我正在使用 JPA,我也想做同样的事情。否则,我必须向schema
每个 @Table 注释添加参数,例如:
@Entity
@Table (name = "projectcategory", schema = "SCHEMANAME")
public class Category implements Serializable { ... }
As I understand this parameter should be somewhere in this part of configuration:
据我了解,这个参数应该在这部分配置的某个地方:
<bean id="domainEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="JiraManager"/>
<property name="dataSource" ref="domainDataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false"/>
<property name="showSql" value="false"/>
<property name="databasePlatform" value="${hibernate.dialect}"/>
</bean>
</property>
</bean>
<bean id="domainDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${db.driver}" />
<property name="jdbcUrl" value="${datasource.url}" />
<property name="user" value="${datasource.username}" />
<property name="password" value="${datasource.password}" />
<property name="initialPoolSize" value="5"/>
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="15"/>
<property name="checkoutTimeout" value="10000"/>
<property name="maxStatements" value="150"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="idleConnectionTestPeriod" value="50"/>
</bean>
... but I can't find its name in google. Any ideas?
...但我在谷歌上找不到它的名字。有任何想法吗?
采纳答案by bert
Don't know of JPA property for this either. But you could just add the Hibernate property (assuming you use Hibernate as provider) as
也不知道 JPA 属性。但是您可以添加 Hibernate 属性(假设您使用 Hibernate 作为提供者)作为
...
<property name="hibernate.default_schema" value="myschema"/>
...
Hibernate should pick that up
Hibernate 应该选择它
回答by granit
In order to avoid hardcoding schema in JPA Entity Java Classes we used orm.xml mapping file in Java EE application deployed in OracleApplicationServer10 (OC4J,Orion). It lays in model.jar/META-INF/ as well as persistence.xml. Mapping file orm.xml is referenced from peresistence.xml with tag
为了避免在 JPA 实体 Java 类中硬编码模式,我们在 OracleApplicationServer10(OC4J,Orion)中部署的 Java EE 应用程序中使用了 orm.xml 映射文件。它位于model.jar/META-INF/ 以及persistence.xml 中。映射文件 orm.xml 是从 peresistence.xml 引用的,带有标记
...
<persistence-unit name="MySchemaPU" transaction-type="JTA">
<provider>
<mapping-file>META-INF/orm.xml</mapping-file>
...
File orm.xml content is cited below:
文件orm.xml内容引用如下:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">
<persistence-unit-metadata>
<persistence-unit-defaults>
<schema>myschema</schema>
</persistence-unit-defaults>
</persistence-unit-metadata>
</entity-mappings>
回答by sernle
Just to save time of people who come to the post (like me, who looking for Spring config type and want you schema name be set by an external source (property file)). The configuration will work for you is
只是为了节省来帖子的人的时间(像我一样,寻找 Spring 配置类型并希望您的架构名称由外部源(属性文件)设置)。配置对你有用的是
<bean id="domainEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="JiraManager"/>
<property name="dataSource" ref="domainDataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false"/>
<property name="showSql" value="false"/>
<property name="databasePlatform" value="${hibernate.dialect}"/>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.default_schema">${yourSchema}</prop>
</props>
</property>
</bean>
Ps : For the hibernate.hdm2ddl.auto, you could look in the post Hibernate hbm2ddl.auto possible values and what they do?I have used to set create-update,because it is convenient. However, in production, I think it is better to take control of the ddl, so I take whatever ddl generate first time, save it, rather than let it automatically create and update.
Ps:对于hibernate.hdm2ddl.auto,您可以查看Hibernate hbm2ddl.auto 可能的值以及它们的作用?我以前设置create-update,因为方便。但是,在生产中,我认为最好控制ddl,因此我将第一次生成的ddl 保存起来,而不是让它自动创建和更新。
回答by WengKit Lei
For others who use spring-boot, java based configuration,
对于使用 spring-boot、基于 java 的配置的其他人,
I set the schema value in application.properties
我在 application.properties 中设置了架构值
spring.jpa.properties.hibernate.dialect=...
spring.jpa.properties.hibernate.default_schema=...
回答by S.Dayneko
For those who uses last versions of spring boot will help this:
对于那些使用最新版本 spring boot 的人会有所帮助:
.properties:
。特性:
spring.jpa.properties.hibernate.default_schema=<name of your schema>
.yml:
.yml:
spring:
jpa:
properties:
hibernate:
default_schema: <name of your schema>
回答by Kader
Use this
用这个
@Table (name = "Test", schema = "\"schema\"")
insteade of @Table (name = "Test", schema = "schema")
而不是 @Table (name = "Test", schema = "schema")
If you are on postgresql the request is :
如果你在 postgresql 上,请求是:
SELECT * FROM "schema".test
not :
不是 :
SELECT * FROM schema.test
PS: Test is a table
PS:测试是一张桌子
回答by theshadog
I had to set the value in '' and ""
我必须在 '' 和 "" 中设置值
spring:
jpa:
properties:
hibernate:
default_schema: '"schema"'