2010-04-14 131 views
0

我在我的Java應用程序中使用hibernate3來訪問sqlserver 2008企業。 hibernate映射使用複合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; 
} 

}

Hibernate映射:

<?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 ?:((
+0

感謝您的信息 – taras 2010-04-14 12:59:41

+0

而這個評論使它更不可能 – Mark 2010-04-14 13:14:23

回答

0

Session.load(...)假定有確實給定id的情況下,大部分時間它會返回一個代理對象而不碰到數據庫。你真正要查詢的是ID爲5的實例,userId = null和date == null。

基本上你會得到一個單元化的代理,並帶有你用來查詢它的複合標識的副本。如果這個實例真的存在,那麼你會好起來的,否則當你第一次嘗試使用這個對象時你會得到一個ObjectNotFoundException異常。