0

我在我的 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

道代码:

AuthorLoginTrack track;
AuthorLoginTrackId trackId; 

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

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

1 回答 1

0

Session.load(...) 假设确实有一个具有给定 id 的实例,大多数情况下它会返回一个代理对象而不访问数据库。您真正要查询的是 id 为 5、userId = null 和 date == null 的实例。

基本上,您将获得一个带有用于查询它的复合 id 副本的统一代理。如果该实例确实存在,您会没事的,否则您将在第一次尝试使用该对象时收到 ObjectNotFoundException。

于 2010-04-14T14:56:04.877 回答