Hibernate没有由db生成主键?

发布于 2024-09-04 17:57:36 字数 3077 浏览 4 评论 0原文

我正在构建一个数据仓库,并希望使用 InfiniDB 作为存储引擎。但是,它不允许主键或外键约束(或与此相关的任何约束)。

当我执行插入时,Hibernate 抱怨“数据库没有返回本机生成的标识值”。

每个表都是关系表,并且包含一个以前用作主键的唯一整数列 - 我想保留它,但只是在数据库中没有该列是主键的约束。

我假设问题是 Hibernate 期望数据库返回生成的密钥。是否可以覆盖此行为,以便我可以自己设置主键字段的值并使 Hibernate 满意?

-- 编辑 --

其中两个映射如下:

<?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>

I'm building a data warehouse and want to use InfiniDB as the storage engine. However, it doesn't allow primary keys or foreign key constraints (or any constraints for that matter).

Hibernate complains "The database returned no natively generated identity value" when I perform an insert.

Each table is relational, and contains a unique integer column that was previously used as the primary key - I want to keep that, but just not have the constraint in the db that the column is the primary key.

I'm assuming the problem is that Hibernate expects the db to return a generated key. Is it possible to override this behaviour so I can set the primary key field's value myself and keep Hibernate happy?

-- edit --

Two of the mappings are as follows:

<?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>

and:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

但可醉心 2024-09-11 17:57:37

正如您在评论中指出的,您可以使用许多 id 生成器。例如,你们很多人都觉得“增量”很方便。完整概述
http://docs. jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-id

并且 hibernate 不会关心您的数据库限制(主键和外键)。事实上,hibernate 无法知道某些数据库限制,直到它被违反。如果限制不存在,就永远不能违反:)

As you noted in a comment, there're many id generators you can use. E.g., you many find 'increment' convenient. Complete overview
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-id

And hibernate won't care about your db restrictions (both primary keys and foreign keys). In fact, hibernate has no way of knowing about certain db restriction until it's violated. And if restriction doesn't exist, it can never be violated :)

假装爱人 2024-09-11 17:57:37

您可以在保存对象之前删除 元素并手动设置 id。

You can remove the <generator class="identity" /> element and set the id manually before saving the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文