是否可以运行配置的Hibernate应用程序hbm2ddl.auto=update
来更新生产环境中的数据库模式?
我发现在hibernate配置文件中我们可以设置参数hibernate.default_schema
:
<hibernate-configuration>
<session-factory>
...
<property name="hibernate.default_schema">myschema</property>
...
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)
现在我正在使用JPA,我想做同样的事情.否则我必须为schema
每个@Table注释添加参数,如:
@Entity
@Table (name = "projectcategory", schema = "SCHEMANAME")
public class Category implements Serializable { ... }
Run Code Online (Sandbox Code Playgroud)
据我所知,这个参数应该在这部分配置的某个地方:
<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 …
Run Code Online (Sandbox Code Playgroud) 我正在查看Hibernate hbm2ddl.auto
配置属性及其可能的值:
validate
update
create
create-drop
所有这些价值观做了什么?
在Hibernate参考文档只是简单地谈约create-drop
,但并没有说明其他任何值:
hibernate.hbm2ddl.auto
SessionFactory
创建时,自动验证或将架构DDL导出到数据库.使用时create-drop
,将在SessionFactory
显式关闭数据库模式时删除它.例如
validate
|update
|create
|create-drop
我在这些Stack Overflow问题中找到了非常有用的解释:
但官方文档中仍然没有任何内容.
什么是默认值
hibernate.hbm2ddl.auto
Run Code Online (Sandbox Code Playgroud)
在hibernate cfg文件映射中
有可能删除
<property name="hibernate.hbm2ddl.auto">update</property>
Run Code Online (Sandbox Code Playgroud)
这个来自配置文件的映射
如果我删除此属性是否会影响我的数据库
???
我正在尝试使用hbm2ddl.auto = update更新现有表.
在几个表中有几列,其中数据库列定义从实体中的声明更改.喜欢
@Column(name="mycolumn", nullable=false, length=10)
private Long mycolumn;
Run Code Online (Sandbox Code Playgroud)
和
'mycolumn'bigint(20)not null默认值为0
在MySQL中.
为什么hbm2ddl不更新这样的东西?是否有可能强制进行此类更新?我想说hbm2ddl删除列的默认值并更改类型的长度.
我正在玩一些JPA的东西,更改映射以了解它们应该是什么样的等等.这是基本的实验.但是我找不到一个简单地读取我的实体然后为我生成表模式的工具.我试图在JBoss工具中找到类似的东西,但是nada.Eclipse集成将是一个巨大的优势,但我会采取命令行工具或蚂蚁任务.
有任何想法吗?
我想第一次在hibernate中创建数据库模式.而且,如果模式中有任何修改,例如添加新表或删除某些列,我想更新现有模式,保持以前的数据不变.
根据此问题给出的选项,看起来我可以创建模式破坏以前的数据,或者我可以更新模式.
有什么价值可以兼得吗?
我有一个@Entity
映射到视图,这是它的外观
import org.hibernate.annotations.Immutable;
import javax.persistence.*;
@Table(name = "user_earning")
@Entity
@Immutable
public class UserFlightEarning {
@Id public Long userId;
public Long flightId;
@Column(name = "flight_seq") public Long flightSequence;
}
Run Code Online (Sandbox Code Playgroud)
这很好用,我可以使用dao从视图中检索记录.但是我在日志中注意到Hibernate实际上是在尝试创建表但由于它已经存在而失败.
2015-11-12 21:56:34.841 ERROR 4204 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport:HHH000389:不成功:create table user_profile(user_id bigint not null,avg_airtime integer,avg_fuel_points integer ,avg_miles integer,email varchar(255),first_name varchar(255),flights_count integer,furthest_flight integer,last_name varchar(255),longest_flight integer,most_visited_city varchar(255),tier_end integer,tier_start integer,primary key(user_id))2015 -11-12 21:56:34.841 ERROR 4204 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport:表'user_profile'已经存在
我可以配置hibernate,以便跳过这些实体的创建吗?我认为@Immutable
注释告诉Hibernate跳过创建,但似乎这个注释只是为了防止表上的crud操作.
在配置hibernate.cfg.xml中,我添加
<property name="hibernate.hbm2ddl.auto">create</property>
Hibernate在运行应用程序时自动创建表.但是,我通过运行drop table sql手动从数据库中删除表.然后再次运行hibernate应用程序.出现异常
引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'test.person'不存在
解决问题的唯一方法是重启Mysql数据库.谁能为我解释这个问题?
这是我的hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Mapping files -->
<mapping resource="com/mapping/Event.hbm.xml" />
<mapping resource="com/mapping/Person.hbm.xml"/>
</session-factory>
Run Code Online (Sandbox Code Playgroud)
谢谢