这里是Hibernate的新手,所以我可能在某处误解和/或犯了错误。
最近我在独立的Java应用程序中实现了Hibernate。我有一个复杂的数据库结构,有很多关联,还有一个超子类构造,每个都有自己的表。问题如下:
当我要求Hibernate从其中一个子类表中选择几行时,我得到的每行ID与数据库中的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 class
更改为assigned
并自行分配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 :(得分:0)
经过一些测试后,我突然意识到Hibernate给出的密钥和数据库中的密钥包含相同的字符,只是顺序不同。在Google上再次搜索之后,我找到了这个主题:
Problem obtaining entities with uniqueidentifier primary key
似乎是这种情况:Java UUID(java.util.UUID)和SQL Server uniqueidentifier是两回事。不知怎的,Hibernate设法映射字符,但不是顺序(尽管两个键的最后部分是相同的)。我通过未被接受作为答案的答案解决了我的问题,并添加了type="uuid-char"
,并且它有效。我还可以让Hibernate使用<generator class="uuid2" />
生成UUID。
我确实想要注意type="uuid-char"
甚至可能不是必需的,因为Hibernate似乎没有它进行映射(尽管它可能令人困惑)。如果使用uuid-char,则在使用Hibernate使用hql / query调用数据库并通过UUID请求行时,还需要将参数转换为String。如果您将UUID作为参数填写,则可能无法获得结果。