我的表格如下:
1- medical_company:
2- account_entity:
3 人:
4- employee_company:
实体:
1- 医疗公司:
@SuppressWarnings("serial")
@Entity
@Table(name = "medical_company")
public class MedicalCompany implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
@Basic(fetch = FetchType.EAGER)
private Long id;
@OneToOne
@Cascade(value = { CascadeType.ALL })
@JoinColumn(name …
Run Code Online (Sandbox Code Playgroud) 我正在使用hibernate注释,我想导出我的数据库模式.
类似于使用hbm xml文件的schemaexporttask.
<property name="hibernate.hbm2ddl.auto">update</property>
Run Code Online (Sandbox Code Playgroud)
我可以创建我的数据库模式,它会自动添加属性,约束,键等......但是更新数据库模式呢?如果我从我的实体中删除一些属性,hibernate不会删除它,或者如果我更改了一些约束,hibernate不会触及已创建的约束...
那么,有一种方法可以让hibernate真正更新数据库模式吗?
谢谢.
我已经更新到更新版本的hibernate3-maven-plugin.尝试使用下面提到的插件时出现以下错误.
非常感谢解决这个问题的任何指示.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<id>create sql schema</id>
<phase>process-test-resources</phase>
<goals>
<goal>hbm2ddl</goal>
</goals>
<configuration>
<componentProperties>
<persistenceunit>${app.module}</persistenceunit>
<drop>false</drop>
<create>true</create>
<outputfilename>${app.sql}-create.sql</outputfilename>
<skip>${db.schema.gen.skip}</skip>
</componentProperties>
</configuration>
</execution>
<execution>
<id>drop sql schema</id>
<phase>process-test-resources</phase>
<goals>
<goal>hbm2ddl</goal>
</goals>
<configuration>
<componentProperties>
<persistenceunit>${app.module}</persistenceunit>
<drop>true</drop>
<create>false</create>
<outputfilename>${app.sql}-drop.sql</outputfilename>
<skip>${db.schema.gen.skip}</skip>
</componentProperties>
</configuration>
</execution>
</executions>
</plugin>
[ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (create sql schema) on project sample: There was an error creating the AntRun task. NullPointerException -> [Help 1]org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (create sql schema) on project framework: …
Run Code Online (Sandbox Code Playgroud) 我想用Ant生成我的数据库模式.我正在使用hbm2ddl任务.
我正在使用Hibernate和JNDI.我的hibernate.cfg.xml看起来像:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.datasource">java:comp/env/jdbc/my_app</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<!-- Mapping -->
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)
我的$ CATALINA_HOME/webapps/myapp/META-INF/context.xml如下所示:
<Context>
<Resource name="jdbc/my_app"
global="jdbc/my_app"
auth="Container"
type="javax.sql.DataSource"
username="userA"
password="userA123"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myapp?autoReconnect=true"
maxActive="8"
maxIdle="4"/>
</Context>
Run Code Online (Sandbox Code Playgroud)
在$ CATALINA_HOME/webapps/myapp/WEB-INF/web.xml里面我有:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/my_app</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Run Code Online (Sandbox Code Playgroud)
在build.xml中我有下一个目标.
<target name="schema" description="Generate DB schema from the O/R mapping files">
<hibernatetool destdir="${basedir}">
<classpath path="${java.src.home}">
<fileset dir="${java.src.home}">
<include name="**/*.hbm.xml"/>
</fileset>
</classpath>
<configuration configurationfile="${java.src.home}/hibernate.cfg.xml"/>
<hbm2ddl
drop="true"
create="true" …
Run Code Online (Sandbox Code Playgroud) 我在Hibernate中将字节数组映射到MySQL数据库时遇到了一些麻烦,并且想知道我是否遗漏了一些明显的东西.我的课看起来大致如下:
public class Foo {
private byte[] bar;
// Getter and setter for 'bar'
}
Run Code Online (Sandbox Code Playgroud)
该表在MySQL 5.5中定义如下:
CREATE TABLE foo (
bar BINARY(64) NOT NULL)
Run Code Online (Sandbox Code Playgroud)
而Hibernate 3.6.2映射看起来类似于:
<hibernate-mapping>
<class name="example.Foo" table="foo">
<property name="bar" column="bar" type="binary" />
</class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)
我正在使用hbm2ddl进行验证,当我部署应用程序时它会给我这个错误:
Wrong column type in foo for column bar. Found: binary, expected: tinyblob
Run Code Online (Sandbox Code Playgroud)
如果在映射中使用type ="binary"不会导致Hibernate期望列的类型为二进制(而不是tinyblob),我不知道会是什么.我花了一些时间谷歌搜索,但无法找到确切的错误.类似错误的解决方案是......
这种设置会导致这种不匹配吗?如果我指定type ="binary"而不是"blob",为什么Hibernate期望blob而不是二进制?
我遇到了hibernate的问题.我最近将我的hbm2ddl设置为validate,并且它一直在抱怨错误的数据类型.除了布尔语,我已解决了所有问题.
opener
我的班级中有一个字段,映射为:
<property column="opener" name="opener" type="boolean"/>
Run Code Online (Sandbox Code Playgroud)
列opener
是a tinyint (4)
并且值为1或0.到目前为止,我已尝试更改类型,但无济于事.我也尝试在hibernate.cfg中使用以下设置:
<property name="hibernate.query.substitutions">true 1, false 0</property>
Run Code Online (Sandbox Code Playgroud)
但我仍然得到同样的错误.我究竟做错了什么?
org.hibernate.HibernateException: Wrong column type: opener, expected: bit
at org.hibernate.mapping.Table.validateColumns(Table.java:261)
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1083)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:116)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:317)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
Run Code Online (Sandbox Code Playgroud)
注意:我无法访问数据库.
我正在使用Arquillian,JBoss,JPA/Hibernate,H2 DB和Maven运行测试.在我的测试persistence.xml文件中,我有:
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
Run Code Online (Sandbox Code Playgroud)
现在我有一个User类通过Hibernate注释映射到'users'表.
事情几乎正常.问题是Hibernate试图执行:
drop table my_schema.users if exists
Run Code Online (Sandbox Code Playgroud)
但架构"my_schema"不存在,因此失败(毕竟我正在对内存数据库运行).
如何让hibernate执行似乎忘记的'create schema my_schema'步骤?
更新: 我从Hibernate看到的消息:
09:42:45,402 INFO [org.jboss.as.jpa] (MSC service thread 1-7) JBAS011402: Starting Persistence Unit Service 'test.war#ccmc'
09:42:45,524 INFO [org.hibernate.annotations.common.Version] (MSC service thread 1-7) HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
09:42:45,532 INFO [org.hibernate.Version] (MSC service thread 1-7) HHH000412: Hibernate Core {4.0.1.Final}
09:42:45,535 INFO [org.hibernate.cfg.Environment] (MSC service thread 1-7) HHH000206: hibernate.properties not found
09:42:45,542 INFO [org.hibernate.cfg.Environment] (MSC service thread 1-7) HHH000021: Bytecode provider …
Run Code Online (Sandbox Code Playgroud) 我希望传递给Hibernate的SessionFactory
hibernate.hbm2ddl.auto=update
Run Code Online (Sandbox Code Playgroud)
并在日志文件中看到生成的sql语句.是不是没有java编码(知道如何用SchemaExport实现结果,但希望hibernate有"in box"解决方案)
在Tomcat Web应用程序上运行的Maven-Spring-Hibernate-MySql中,我正在使用hibernate ddl来生成MySQL5InnoDBDialect的数据库模式.
除了外键的级联选项之外,生成的模式很好.例如,我有这样的结构:
保存用户详细信息对象的用户对象,两者共享相同的密钥:
@Entity
@Table(name = "Users")
public class User implements Serializable {
private static final long serialVersionUID = -359364426541408141L;
/*--- Members ---*/
/**
* The unique generated ID of the entity.
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "User_Id")
protected long id;
@Getter
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "user", optional = true)
protected UserDetails userDetails;
...
}
Run Code Online (Sandbox Code Playgroud)
而用户详细信息:
@Entity
@Table(name = "UserDetails")
public class UserDetails implements Serializable {
private static final long serialVersionUID = …
Run Code Online (Sandbox Code Playgroud)