Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在JPA配置中设置默认模式名?_Java_Hibernate_Spring_Configuration_Jpa - Fatal编程技术网

Java 如何在JPA配置中设置默认模式名?

Java 如何在JPA配置中设置默认模式名?,java,hibernate,spring,configuration,jpa,Java,Hibernate,Spring,Configuration,Jpa,我发现在hibernate配置文件中可以设置参数hibernate.default\u schema: <hibernate-configuration> <session-factory> ... <property name="hibernate.default_schema">myschema</property> ... </session-factory> </hiber

我发现在hibernate配置文件中可以设置参数
hibernate.default\u schema

<hibernate-configuration> 
   <session-factory>
      ...
      <property name="hibernate.default_schema">myschema</property>
      ...
   </session-factory>
</hibernate-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>


。。。但我在谷歌上找不到它的名字。有什么想法吗?

我也不知道JPA的财产。但是您可以将Hibernate属性(假设您使用Hibernate作为提供程序)添加为

。。。
...

Hibernate应该注意这一点,以避免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>
...
。。。
META-INF/orm.xml
...
文件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>

迈斯切玛

这只是为了节省来这篇文章的人的时间(比如我,他们正在寻找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>

没有一个
${yourSchema}
附言: 对于hibernate.hdm2ddl.auto,您可以查看帖子
我曾经设置过创建更新,因为它很方便。然而,在生产环境中,我认为最好控制ddl,所以我将第一次生成的ddl保存起来,而不是让它自动创建和更新

对于其他使用基于java的spring boot配置的用户

我在application.properties中设置了模式值

spring.jpa.properties.hibernate.dialect=...
spring.jpa.properties.hibernate.default_schema=...

对于那些使用最新版本的spring boot的用户,这将有助于:

.物业:

spring.jpa.properties.hibernate.default_schema=<name of your schema>
spring.jpa.properties.hibernate.default\u模式=
.yml:

spring:
jpa:
特性:
冬眠:
默认_模式:
使用此

@Table (name = "Test", schema = "\"schema\"")
而不是
@Table(name=“Test”,schema=“schema”)

如果您使用postgresql,则请求为:

SELECT * FROM "schema".test 
不是:

SELECT * FROM schema.test
PS:Test是一个表

我必须在“”和“”中设置值

如果您使用
ApplicationContext.xml
中的
(org.springframework.jdbc.datasource.DriverManagerDataSource)
指定数据库详细信息,则使用下面的简单属性指定架构

<property name="schema" value="schemaName" />


应添加到
persistence.xml中persistent unit的
部分。很抱歉,您没有看到persistence.xml,但使用Spring配置Hibernate/JPA。我会尝试将其作为属性添加到HibernateJavaEndorapter下。甚至可能没有名字前面的“hibernate”。这里的问题是我不能使用Spring文件中的属性,比如${jdbc.schema}或类似的属性。在@formula元素中,hibernate不包括shema,并使查询在sqlserver中失败我更喜欢此解决方案,因为它是标准的,而不是特定于实现的。我得到了ParseExceptionno-需要从持久性单元META-INF/orm.xml中删除提供程序元素。。。
SELECT * FROM "schema".test 
SELECT * FROM schema.test
spring:
   jpa:
     properties:
       hibernate:
          default_schema: '"schema"'
<property name="schema" value="schemaName" />