2010-04-14 7 views
0

J'utilise hibernate3 dans mon application java pour accéder à sqlserver 2008 enterprise. Le mappage d'hibernation utilise l'ID composite et lorsque j'essaie de charger le modèle, il renvoie null. J'ai passé des jours à le résoudre mais toujours pas de résultat. Le mappage d'ID composite doit être utilisé pour plusieurs PK basées sur des champs, mais dans ma table pas de PK, je me demande pourquoi l'outil Jiboss Hibernate (plugin eclipse) l'a généré avec un mappage d'ID composite?Problème de mappage Hibernate avec l'ID composite

J'apprécierais n'importe quelle aide ou commentaires.

Modèles Hibernate:

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"> 
<!-- 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> 

Tableau 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 

Tableau:

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

Code 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

Merci pour votre info – taras

+0

Et ce commentaire le rend encore moins probable – Mark

Répondre

0

Session.load (...) suppose qu'il y a en effet une instance avec l'identifiant donné, la plupart du temps, il retourne un objet proxy sans toucher à la base de données. Ce que vous demandez vraiment, c'est une instance avec l'ID 5, userId = null et date == null.

Fondamentalement, vous obtenez un proxy unitialized avec une copie de l'identifiant composé que vous avez utilisé pour l'interroger. Si l'instance existe réellement, tout ira bien, sinon vous obtiendrez une ObjectNotFoundException la première fois que vous essaierez d'utiliser l'objet.

Questions connexes