簡體   English   中英

帶有Composite-ID的Hibernate映射問題

[英]Hibernate mapping issue with composite-id

我在Java應用程序中使用hibernate3訪問sqlserver 2008企業版。 休眠映射使用復合ID,當我嘗試加載模型時,它返回null。 我花了幾天時間解決它,但仍然沒有結果。 復合ID映射應該用於基於多字段的PK,但是在我的表中沒有這樣的PK,我想知道為什么JBoss Hibernate Tool(eclipse插件)使用復合ID映射生成了它?

我將不勝感激任何幫助或意見。

休眠模型:

public class AuthorLoginTrack implements java.io.Serializable {

private AuthorLoginTrackId id;

public AuthorLoginTrack() {
}

public AuthorLoginTrack(AuthorLoginTrackId id) {
    this.id = id;
}

public AuthorLoginTrackId getId() {
    return this.id;
}

public void setId(AuthorLoginTrackId id) {
    this.id = id;
}

}

public class AuthorLoginTrackId implements java.io.Serializable {

private long id;
private String userId;
private Date dateCreated;

public AuthorLoginTrackId() {
}

public AuthorLoginTrackId(long id, String userId) {
    this.id = id;
    this.userId = userId;
}

public AuthorLoginTrackId(long id, String userId, Date dateCreated) {
    this.id = id;
    this.userId = userId;
    this.dateCreated = dateCreated;
}

public long getId() {
    return this.id;
}

public void setId(long id) {
    this.id = id;
}

public String getUserId() {
    return this.userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public Date getDateCreated() {
    return this.dateCreated;
}

public void setDateCreated(Date dateCreated) {
    this.dateCreated = dateCreated;
}

public boolean equals(Object other) {
    if ((this == other))
        return true;
    if ((other == null))
        return false;
    if (!(other instanceof AuthorLoginTrackId))
        return false;
    AuthorLoginTrackId castOther = (AuthorLoginTrackId) other;

    return (this.getId() == castOther.getId())
            && ((this.getUserId() == castOther.getUserId()) || (this
                    .getUserId() != null
                    && castOther.getUserId() != null && this.getUserId()
                    .equals(castOther.getUserId())))
            && ((this.getDateCreated() == castOther.getDateCreated()) || (this
                    .getDateCreated() != null
                    && castOther.getDateCreated() != null && this
                    .getDateCreated().equals(castOther.getDateCreated())));
}

public int hashCode() {
    int result = 17;

    result = 37 * result + (int) this.getId();
    result = 37 * result
            + (getUserId() == null ? 0 : this.getUserId().hashCode());
    result = 37
            * result
            + (getDateCreated() == null ? 0 : this.getDateCreated()
                    .hashCode());
    return result;
}

}

休眠映射:

<?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 Apr 6, 2010 4:17:46 PM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
    <class name="com.entity.model.AuthorLoginTrack" table="AuthorLoginTrack" schema="dbo" catalog="tribetoyota_db">
        <composite-id name="id" class="com.entity.model.AuthorLoginTrackId">
            <key-property name="id" type="long">
                <column name="ID" precision="18" scale="0" />
            </key-property>
            <key-property name="userId" type="string">
                <column name="UserID" length="20" />
            </key-property>
            <key-property name="dateCreated" type="timestamp">
                <column name="DateCreated" length="16" />
            </key-property>
        </composite-id>
    </class>
</hibernate-mapping>

表轉儲:

/****** Object:  Table [dbo].[AuthorLoginTrack]    Script Date: 04/14/2010 20:43:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[AuthorLoginTrack](
    [ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
    [UserID] [varchar](20) NOT NULL,
    [DateCreated] [smalldatetime] NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[AuthorLoginTrack] ADD  CONSTRAINT [DF_AuthorLoginTrack_DateCreated]  DEFAULT (getdate()) FOR [DateCreated]
GO

表:

ID  UserID   DateCreated
------------------------------------
5   cooler   2005-03-17 18:56:00
6   miumiu   2005-03-17 19:46:00

DAO代碼:

AuthorLoginTrack track;
AuthorLoginTrackId trackId; 

trackId = new AuthorLoginTrackId();
trackId.setId(5);

track = (AuthorLoginTrack)getSession().load(AuthorLoginTrack.class, trackId);
return track.getId().getUserId(); // returns null why ?:((

Session.load(...)假定確實存在一個具有給定id的實例,大多數情況下,它將在不訪問數據庫的情況下返回代理對象。 您真正要查詢的是一個ID為5的實例,userId = null和date == null。

基本上,您將獲得一個統一代理,其中包含用於查詢它的復合ID的副本。 如果實例確實存在,則可以,否則,第一次嘗試使用該對象時,將獲得ObjectNotFoundException。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM