我正在使用现有的数据库架构处理遗留代码库.现有代码使用SQL和PL/SQL在DB上执行查询.我们的任务是使项目数据库引擎不可知的一小部分(最初,最终改变一切).我们选择使用Hibernate 3.3.2.GA和"*.hbm.xml"映射文件(而不是注释).遗憾的是,更改现有架构是不可行的,因为我们无法回退任何遗留功能.
我遇到的问题是当我试图映射单向,一对多的关系时,FK 也是复合PK的一部分.这是类和映射文件......
CompanyEntity.java
public class CompanyEntity {
private Integer id;
private Set<CompanyNameEntity> names;
...
}
Run Code Online (Sandbox Code Playgroud)
CompanyNameEntity.java
public class CompanyNameEntity implements Serializable {
private Integer id;
private String languageId;
private String name;
...
}
Run Code Online (Sandbox Code Playgroud)
CompanyNameEntity.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.jboss.org/dtd/hibernate/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.example">
<class name="com.example.CompanyEntity" table="COMPANY">
<id name="id" column="COMPANY_ID"/>
<set name="names" table="COMPANY_NAME" cascade="all-delete-orphan" fetch="join" batch-size="1" lazy="false">
<key column="COMPANY_ID"/>
<one-to-many entity-name="vendorName"/>
</set>
</class>
<class entity-name="companyName" name="com.example.CompanyNameEntity" table="COMPANY_NAME">
<composite-id>
<key-property name="id" column="COMPANY_ID"/>
<key-property name="languageId" …
Run Code Online (Sandbox Code Playgroud) 我试图在HBM文件中创建一个包含Enum作为字段的类.
HBM与此类似:
<class name="a.b.c.myObject" table="OBJECT" >
<property name="myEnum" column="EXAMPLE" type="a.b.c.myEnum" />
</class>
Run Code Online (Sandbox Code Playgroud)
让我们说这是Enum:
public enum myEnum{
a, b, c;
}
Run Code Online (Sandbox Code Playgroud)
问题是在DB中我期望看到该枚举的字符串值(a,b或c),但我获得了该字段的原始数据.
我怎么解决这个问题?
我有两个表,tableA和tableB.
tableA有列:tabAId,col2,col3 (tabAId primaryKey和Identity列.)
tableB有列:tabAId,name (tabAId不为null)
我在tableA的hbm文件中创建了Bag,以维护关系.
<bag name="tableB" lazy="true" inverse="false"
batch-size="25" cascade="all-delete-orphan">
<key column="tabAId" />
<one-to-many class="tableB" />
</bag>
Run Code Online (Sandbox Code Playgroud)
当我尝试更新tableA中的记录时,它抛出异常,因为我在tableA实例中有子列表.
[NHibernate.Exceptions.GenericADOException] = {"无法删除集合:[MIHR.Entities.tableA.tableB#21] [SQL:UPDATE dbo.tableB SET tabAId = null WHERE tabAId = @ p0]"}
InnerException = {"无法将值NULL插入列'tabAId',表'SA_MIHR_DEV.dbo.tableB';列不允许空值.UPDATE失败.\ r \n语句已终止."}
我在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)
注意:我无法访问数据库.
我正在尝试调试我的流畅应用程序,并希望看到生成的文件是什么样的.
有什么方法可以查看它们?
有没有办法出口它们?
这是我的heirarchy:
class abstract Entity { /*members*/ } // mapped to entity table class abstract User : Entity { /*members*/ } // mapped to user table class Employee : User { /*no members*/ } // no table, discriminator = "E" class Contractor : User { /*no members*/ } // no table, discriminator = "C"
这是我在两个单独的hbm文件中的映射:
<class name="Entity" table="entity" xmlns="urn:nhibernate-mapping-2.2"> <id name="Id" column="id"> <generator class="guid.comb" /> </id> <property ... /> </class> <joined-subclass name="User" extends="Entity" table="user"> <key column="id" /> <discriminator column="type" /> <property …
在我的Java应用程序中,我使用hibernate .hbm文件来访问数据库; 是否可以更新表中的主键"id"列; 我的.hbm文件中的'id'列如下:
<hibernate-mapping package="org.jems.user.model">
<class name="Student_Details" table="t_student">
<id name="id" type="int" column="id">
<generator class="increment"/>
</id>
<property name="name" column="name" type="string" unique="true" not-null="true" />
<property name="description" column="description" type="string" />
<property name="comments" column="comments" type="string" />
<property name="active" column="isActive" type="boolean" not-null="true" />
</class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud) 我正在使用HQL命名查询(在XML文件中定义)来使用Hibernate查询我的数据库.有些查询非常复杂,我发现自己将一个查询的大部分内容复制粘贴到另一个查询,类似的查询.
我想知道是否有可能在"命名查询片段"中定义公共部分并在我的所有查询中重用该片段?
我不想使用标准API,顺便说一句,因为我觉得在XML中制定查询更加舒服.其中一些已经是一个非常怪物,用API实现它们会使它们更加难以理解.