2016-07-06 87 views
0

这里是Hibernate的新手,所以可能是因为我误解和/或在某个地方犯了错误。Hibernate生成新的Id而不是从数据库获取Id?

最近,我在独立的Java应用程序中实现了Hibernate。我有一个复杂的数据库结构,有很多关联,还有一个超级子类构造,每个表都有自己的表。问题如下:

当我要求Hibernate从其中一个子类表中选择几行时,我得到的每行ID与数据库中的不一样。当我想在其他地方使用此Id(获取外键违规)时,会导致很多问题。尽管我无法在数据库中的任何位置找到Id,但在同一张表中或其他位置中,Id始终与该特定行相同。

我的Hibernate映射为超/子如下:

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

如果我改变generator classassigned并分配UUID我自己,我仍然得到了同样的问题。

子类的一个实例:

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()); 
} 
} 

超类:

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; 
} 
} 

插入的一个子类作品完美,我没有要插入的自动进入超。 注意:我不完全确定,如果我的问题只是因为超级/子类的构造,但这是我第一次遇到一些问题。

我错过了什么或没有正确理解关于Hibernate的知识?

+0

更正:我有一个随机的不同表的问题,它没有超/子类的构造。 – Century

回答

0

经过一些更多的测试后,我突然意识到Hibernate给出的关键字和数据库中的关键字包含相同的字符,只是顺序不同而已。在谷歌其他搜索之后,我发现这个话题:

Problem obtaining entities with uniqueidentifier primary key

看来,这样的话:唯一标识符是两回事Java的UUID(java.util.UUID中)和SQL Server。不知怎的,Hibernate设法映射字符,但不是顺序(尽管两个键的最后部分是相同的)。我通过没有被接受为答案的答案解决了我的问题,并添加了type="uuid-char",并且它工作正常。我也可以让Hibernate用<generator class="uuid2" />生成UUID。

我想注意的是,type="uuid-char"甚至可能没有必要,因为Hibernate似乎没有它的映射(尽管它可能会令人困惑)。如果使用uuid-char,则在使用Hibernate使用hql/query调用数据库并通过UUID询问行时,还必须将参数强制转换为String。如果您填入UUID作为参数,则可能无法获得结果。