题
我正在建造一个数据仓库,想使用 Infinidb 作为存储引擎。但是,它不允许主键或外键约束(或此事的任何约束)。
Hibernate抱怨“当我执行插入内容时,数据库没有返回本生成的身份值”。
每个表都是关系的,并且包含一个以前用作主要键的唯一整数列 - 我想保留它,但是只是在DB中没有限制列是主键。
我假设问题是Hibernate期望DB返回生成的密钥。是否可以覆盖这种行为,以便我自己设置主要的钥匙字段的价值并保持冬眠?
- 编辑 -
其中两个映射如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 1, 2010 2:49:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.example.project.Visitor" table="visitor" catalog="orwell">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="firstSeen" type="timestamp">
<column name="first_seen" length="19" />
</property>
<property name="lastSeen" type="timestamp">
<column name="last_seen" length="19" />
</property>
<property name="sessionId" type="string">
<column name="session_id" length="26" unique="true" />
</property>
<property name="userId" type="java.lang.Long">
<column name="user_id" />
</property>
<set name="visits" inverse="true">
<key>
<column name="visitor_id" />
</key>
<one-to-many class="com.example.project.Visit" />
</set>
</class>
</hibernate-mapping>
和:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 1, 2010 2:49:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.example.project.Visit" table="visit" catalog="orwell">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="visitor" class="com.example.project.Visitor" fetch="join" cascade="all">
<column name="visitor_id" />
</many-to-one>
<property name="visitId" type="string">
<column name="visit_id" length="20" unique="true" />
</property>
<property name="startTime" type="timestamp">
<column name="start_time" length="19" />
</property>
<property name="endTime" type="timestamp">
<column name="end_time" length="19" />
</property>
<property name="userAgent" type="string">
<column name="user_agent" length="65535" />
</property>
<set name="pageViews" inverse="true">
<key>
<column name="visit_id" />
</key>
<one-to-many class="com.example.project.PageView" />
</set>
</class>
</hibernate-mapping>
解决方案
正如您在评论中指出的那样,您可以使用许多ID生成器。例如,您发现“增量”方便。完成概述
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mpapping-mapping-declaration-id
Hibernate不在乎您的DB限制(主要键和外键)。实际上,在侵犯冬眠之前,冬眠无法了解某些DB限制。如果不存在限制,就永远不会违反:)
其他提示
您可以删除 <generator class="identity" />
元素并在保存对象之前手动设置ID。
不隶属于 StackOverflow