简体   繁体   中英

Hibernate generates new Id instead of getting Id from database?

New to Hibernate here, so it might be that I just misunderstood and/or made a mistake somewhere.

Recently I implemented Hibernate in my standalone Java application. I have a complicated database structure with a lot of associations, and one super-subclass-construction, with each their own tables. The problem is as follows:

When I ask Hibernate to select a couple of rows from one of the subclass-tables, I get an Id per row that is not the same as the one in the database. This results in a lot of problems when I want to use this Id elsewhere (getting foreign key violations). The Id is always the same for that specific row, though I cannot find the Id anywhere in the database, not in the same table or in others.

My Hibernate mapping for the super/subclass is as follows:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="bliksem_prototype.model.Endpoint" table="Bliksem_Endpoint" schema="dbo" catalog="WMB_Application_Operations_ONTW" optimistic-lock="version">
    <id name="endpointId">
        <column name="EndpointId" length="36" />
        <generator class="uuid2" />
    </id>
    <property name="endpointType" type="string">
        <column name="EndpointType" length="30" not-null="true" />
    </property>
    <set name="endpointFlows" table="Bliksem_Link_Endpoint_Flow" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="EndpointId" length="36" not-null="true" />
        </key>
        <one-to-many class="bliksem_prototype.model.EndpointFlow" />
    </set>
    <joined-subclass name="bliksem_prototype.model.Queue" table="Bliksem_Queue" schema="dbo" catalog="WMB_Application_Operations_ONTW" extends="bliksem_prototype.model.Endpoint">
        <key column="QueueId"/>
        <many-to-one name="cluster" class="bliksem_prototype.model.Cluster" fetch="select">
            <column name="ClusterName" length="30" />
        </many-to-one>
        <many-to-one name="queueManager" class="bliksem_prototype.model.QueueManager" fetch="select">
            <column name="QueueManagerName" length="30" not-null="true" />
        </many-to-one>
        <property name="queueName" type="string">
            <column name="QueueName" length="70" not-null="true" />
        </property>
        <property name="queueType" type="string">
            <column name="QueueType" length="30" not-null="true" />
        </property>
        <property name="maxDepth" type="java.lang.Long">
            <column name="MaxDepth" precision="10" scale="0" />
        </property>
        <property name="maxMessageLength" type="java.lang.Long">
            <column name="MaxMessageLength" precision="15" scale="0" />
        </property>
        <set name="queueDestinationRouters" table="Bliksem_Link_Queue_DestinationRouter" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="QueueId" length="36" not-null="true" />
            </key>
            <one-to-many class="bliksem_prototype.model.QueueDestinationRouter" />
        </set>
    </joined-subclass>
    <joined-subclass name="bliksem_prototype.model.FileNode" table="Bliksem_FileNode" schema="dbo" catalog="WMB_Application_Operations_ONTW" extends="bliksem_prototype.model.Endpoint">
        <key column="FileNodeId"/>
        <property name="fileNodeName" type="string">
            <column name="FileNodeName" length="70" not-null="true" />
        </property>
        <property name="mapName" type="string">
            <column name="MapName" length="70" />
        </property>          
    </joined-subclass> 
    <joined-subclass name="bliksem_prototype.model.Webservice" table="Bliksem_Webservice" schema="dbo" catalog="WMB_Application_Operations_ONTW" extends="bliksem_prototype.model.Endpoint">
        <key column="WebserviceId"/>
        <property name="webserviceName" type="string">
            <column name="WebserviceName" length="70" not-null="true" />
        </property>
    </joined-subclass>
</class>
</hibernate-mapping>

If I change the generator class to assigned and assign the UUID myself, I still get the same problem.

Example of one of the subclasses:

public class FileNode extends Endpoint implements java.io.Serializable,      Comparable<FileNode> {


 private String fileNodeName;
 private String mapName;

public FileNode() {
}

public FileNode(UUID fileNodeId, String fileNodeName, String mapName) {
    super(fileNodeId, "FileNode");
    this.fileNodeName = fileNodeName;
    this.mapName = mapName;
}

public FileNode(UUID fileNodeId, String fileNodeName) {
    super(fileNodeId, "FileNode");
    this.fileNodeName = fileNodeName;
}

public String getFileNodeName() {
    return this.fileNodeName;
}

public void setFileNodeName(String fileNodeName) {
    this.fileNodeName = fileNodeName;
}
public String getMapName() {
    return this.mapName;
}

public void setMapName(String mapName) {
    this.mapName = mapName;
}

@Override
public int compareTo(FileNode o) {
    return this.getFileNodeName().compareTo(o.getFileNodeName());
}
}

Superclass:

public class Endpoint implements java.io.Serializable {


 private UUID endpointId;
 private String endpointType;
 private Set endpointFlows = new HashSet(0);
 @Transient
 private String environmentName;

public Endpoint() {
}

public Endpoint(UUID endpointId, String endpointType, Set endpointFlows) {
    this.endpointId = endpointId;
    this.endpointType = endpointType;
    this.endpointFlows = endpointFlows;
}

public Endpoint(UUID endpointId, String endpointType) {
    this.endpointId = endpointId;
    this.endpointType = endpointType;
}

public Endpoint(UUID endpointId, String endpointType, String environmentName) {
    this.endpointId = endpointId;
    this.endpointType = endpointType;
    this.environmentName = environmentName;
}

public UUID getEndpointId() {
    return endpointId;
}

public void setEndpointId(UUID endpointId) {
    this.endpointId = endpointId;
}

public String getEndpointType() {
    return this.endpointType;
}

public void setEndpointType(String endpointType) {
    this.endpointType = endpointType;
}

public String getEnvironmentName() {
    return environmentName;
}

public void setEnvironmentName(String environmentName) {
    this.environmentName = environmentName;
}

public Set getEndpointFlows() {
    return endpointFlows;
}

public void setEndpointFlows(Set endpointFlows) {
    this.endpointFlows = endpointFlows;
}
}

Inserting one of the subclasses works perfectly, I don't have to insert the superclass which goes automatically. Note: I'm not entirely sure if my problem is only because of the super/subclass construction, but this is the first time I ran across some problems with it.

What am I missing or not understanding correctly about Hibernate?

After some more testing, I suddenly realized that the key given by Hibernate and the one in the database contained the same characters, just in a different order. After another search on Google, I found this topic:

Problem obtaining entities with uniqueidentifier primary key

It seems that this is the case: The Java UUID (java.util.UUID) and the SQL Server uniqueidentifier are two different things. Somehow Hibernate manages to map the characters, but not the order (though the final part of both keys were the same). I solved my problem by the answer that wasn't accepted as an answer, and added type="uuid-char" , and it works. I can also let Hibernate generate the UUID with <generator class="uuid2" /> .

I do want to note that the type="uuid-char" might not even be necessary, as Hibernate does seem to do mapping without it (though it might be confusing). If you use uuid-char, it is also necessary to cast parameters to String when calling the database with hql/query using Hibernate and asking for a row by the UUID. If you fill in the UUID as parameter, you might not get a result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM