javahibernateorminfinidb

Hibernate without primary keys generated by db?


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>

Solution

  • 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 :)