我正在为我们的oracle生产数据库配置一个h2测试数据库.所有表都是模式xxx.我的数据源定义如下:
public DataSource dataSource() {
JdbcDataSource ds = new JdbcDataSource();
ds.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=Oracle;TRACE_LEVEL_SYSTEM_OUT=2;INIT=CREATE SCHEMA IF NOT EXISTS xxx;SCHEMA=xxx");
ds.setUser("xxx");
ds.setPassword("xxx");
return ds;
}
Run Code Online (Sandbox Code Playgroud)
有了SCHEMA=xxx,我收到一个错误:Caused by: org.h2.jdbc.JdbcSQLException: Schema "xxx" not found; SQL statement: SET SCHEMA xxx [90079-186]
如果没有SCHEMA=xxx,我会在Hibernate尝试使用连接运行查询时收到错误,因为它不会将模式添加到表名中.虽然它在我们的oracle数据库的生产中这样做.
编辑:为了提供更多的见解,我从生产中使用的创建脚本填充我的数据库:
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
final DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource);
initializer.setDatabasePopulator(databasePopulator());
return initializer;
}
private DatabasePopulator databasePopulator() {
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.setSeparator(";");
populator.setCommentPrefix("--");
populator.addScript(new ClassPathResource("db-schema.sql"));
populator.addScript(new ClassPathResource("db-init-data.sql"));
return populator;
}
Run Code Online (Sandbox Code Playgroud) 我正在构建一个spring MVC Web应用程序.我使用Hibernate作为我的ORM.每当我重新启动tomcat 7服务器时,它都会自动删除之前创建的表.怎么预防这个?
我一直在尝试使用hibernate和spring以及servlet.现在,我被困住了.为什么我得到这个例外?我认为当hbm2ddl.auto设置为create时会自动创建表.
appicationContext.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url" value="jdbc:derby://localhost:1527/db;create=true" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="org.springpractice.domain" />
<property name="hibernateProperties">
<props>
<prop key="dialect">
org.hibernate.dialect.DerbyTenSevenDialect</prop>
<prop key="show_sql">true</prop>
<prop key="cache.provider_class">
org.hibernate.cache.NoCacheProvider</prop>
<prop key="hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
UserDetails.java
package org.springpractice.domain;
@Entity
public class UserDetails {
@Id @GeneratedValue
protected int id;
protected String name;
protected String email;
// setters/getters ...
}
Run Code Online (Sandbox Code Playgroud)
Main.java
@WebServlet("/")
public class Main extends HttpServlet {
private static final long serialVersionUID = 1L;
protected …Run Code Online (Sandbox Code Playgroud) 我有春天项目,我正在使用休眠.当我开始项目时,db没有变化.
我尝试了difrenf配置,但没有工作.
什么想法可能是错的?
Hibernate在servlet.xml中配置:
<beans:beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:beans="http://www.springframework.org/schema/beans">
<context:component-scan base-package="com.springapp.mvc"/>
<!--Data source has the database information -->
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="org.postgresql.Driver"/>
<beans:property name="url" value="jdbc:postgresql://localhost:5432/Praktyki"/>
<beans:property name="username" value="dbusersolsoft"/>
<beans:property name="password" value="dbpassSolsoft"/>
</beans:bean>
<!-- Session Factory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="packagesToScan">
<value>com.springapp.mvc.model</value>
</beans:property>
<!--<beans:property name="showSql" value="true" />-->
<!--<beans:property name="generateDdl" value="true" />-->
<beans:property name="hibernateProperties">
<beans:props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
</beans:props>
</beans:property>
</beans:bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
Run Code Online (Sandbox Code Playgroud)
我正在使用 liquibase 在我的 springboot 应用程序中初始化我的数据库,它工作正常,直到我重新启动 - 数据库重新初始化并擦除所有数据。
这是我的应用程序属性
# Liquibase
liquibase.change-log=classpath:/db/changelog/iot-db.xml
liquibase.check-change-log-location=true
# Hibernate
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=
entitymanager.packagesToScan=com.whatever
Run Code Online (Sandbox Code Playgroud)
是否有属性允许我创建持久数据库而不是内存数据库?
我正在尝试使用MySQL数据库启动spring boot项目,但是数据库存在一些问题。我尝试启动我的应用程序,并且服务器正在运行,但是休眠时不创建Tables等。
这是我的代码:
用户实体
@Entity
public class User {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
private String firstName;
private String lastName;
private String email;
private String password;
private String description;
private String profile_photo;
private LocalDate create;
private LocalDate update;
@OneToMany(mappedBy = "eventOwner")
private List<Event> ownedEvents;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String …Run Code Online (Sandbox Code Playgroud) 我们在hibernate和hsqldb中自动创建ddl时遇到了一些问题.
我们用
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
Run Code Online (Sandbox Code Playgroud)
通常,当我们更改映射对象时,hibernate会自动在db上的模式中创建表,但有时它会拒绝这样做.在这种情况下,我们必须从文件系统手动删除数据库,以说服hibernate重新创建所有表.
有谁知道hibernate如何决定何时在db上重新创建数据模型?我已经读过,它决定在创建SessionFactory时,但是说服hiberbate更新或创建表的确切条件是什么?
我正在使用Spring 3和hibernate 4.
这是我的root-context.xml
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/musicstore"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="sessionFactory" name="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan" value="domain" /><!--
entity -->
</bean>
Run Code Online (Sandbox Code Playgroud)
我有这个:
WARN : org.hibernate.engine.jdbc.internal.JdbcServicesImpl - HHH000342: Could not obtain connection to query metadata : Unknown database 'musicstore'
Run Code Online (Sandbox Code Playgroud)
当我在tomcat中部署我的项目时,我希望hibernate会在不存在的情况下创建模式.我试过hibernate.hbm2ddl.auto = create但它没有用
有没有办法在运行时自动创建架构?任何建议都会有所帮助:D
提前致谢.
我有一个Java应用程序,它使用JDBC将数据从旧文件格式加载到SQLite数据库中。如果指定的数据库文件不存在,则应创建一个新的数据库文件。当前,数据库的架构已在应用程序中进行了硬编码。我宁愿将它作为SQL脚本保存在单独的文件中,但是显然没有简单的方法可以通过JDBC执行SQL脚本。还有其他方法或模式可以实现这样的目标吗?
我的项目有Hibernate,
但还没有春天.
我怀疑是否:
问题1:
persistence.xml中的Hibernate注释和方言属性足以让Hibernate生成表吗?
问题2:我猜也是,必须有类似Spring的东西来寻找注释并解雇生成事件并开始其他一些事情吗?
编辑:
我的persistence.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Kutuphane2" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>kutuphaneDS</non-jta-data-source>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/kutuphane"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud) 假设我已经有了一个有效的Spring项目,那么使用Spring 3 XML配置在Hibernate 4中添加的最小配置量是多少?我想使用基于注释的事务管理,并使用注释来映射我的对象.
注意:这是一个自我回答问答风格的问题,旨在为常见问题提供规范的答案.我打算随着时间的推移扩展这个问题,以便与Hibernate保持同步.
我使用的是JavaSE + JPA + Hibernate + HSQLDB.
我的持久性xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="persistence" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>sk.tokra.jpa.model.TestDB</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:C:\Dev\db\hsqldb_db" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
我创建表,把对象放在那里,findObject by id返回我的obj [ok],我停止app - 下一步启动应用程序读取,得到我null对象,当我找到id(表是空的).
我以为:
<property name="hibernate.hbm2ddl.auto" value="create"/>
Run Code Online (Sandbox Code Playgroud)
不要放我的桌子,
有任何想法吗 ?
我正在尝试使用 MySQL 和 SpringBoot 为员工创建登录名。我使用内存数据库使我的代码工作,但是一旦我将代码切换到 MySQL,它就停止工作。这个项目是一个科学怪人,所以我不确定我的一些组件是否可以一起工作。我不确定是否需要我的 App 类中的所有注释...错误如下:
描述:
Field employeeRepository in io.msela.springbootstarter.employee.EmployeeService
required a bean named 'entityManagerFactory' that could not be found.
Run Code Online (Sandbox Code Playgroud)
行动:
考虑定义一个'entityManagerFactory'在您的配置中命名的 bean 。
这是我的App课程,它运行整个过程:
package io.msela;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import io.msela.springbootstarter.employee.EmployeeRepository;
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) //added for MySQL
@ComponentScan({"io.msela"})
@EntityScan("io.msela.springbootstarter")
@EnableJpaRepositories(basePackageClasses = EmployeeRepository.class)
public class EmployeeApiDataApplication {
public static void main(String[] args) {
SpringApplication.run(EmployeeApiDataApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
这是 …