views:

402

answers:

1

Hi

I am using hibernate3 in my java app to access sqlserver 2008 enterprise. The hibernate mapping uses composite id and when i try to load model it returns null. I spent days to resolve it but still no result. Composite id mapping should be used for multiple field based PK, but in my table no such PK, i wonder why the JBoss Hibernate Tool(eclipse plugin) generated it with composite id mapping ?

I would appreciate any help or comments.

Hibernate Models:

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 Mapping:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
<!-- 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>

Table Dump:

/****** 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

Table:

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

DAO Code:

AuthorLoginTrack track;
AuthorLoginTrackId trackId; 

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

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

Hi,

Session.load(...) assumes that there's indeed an instance with the given id, most of the time it will return a proxy object without hitting the database. What you're really querying is an instance with id 5, userId = null and date == null.

Basically you're getting an unitialized proxy with a copy of the composite id you used to query it. If the instance really exists you'll be fine, otherwise you'll get an ObjectNotFoundException the first time you'll try to use the object.

Guillaume