-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: mapping a m2m unidirectional relation as m-1
PostPosted: Fri Mar 06, 2009 12:29 pm 
Newbie

Joined: Mon Oct 11, 2004 1:30 pm
Posts: 15
Location: New York, NY
Hibernate Core Version
Hibernate 3.3.1.GA
Hibernate EntityManager 3.3.2.GA

Mapping documents:
Code:
class Version {
    @Id
    private Integer id;

    @Column(name="version_name")
    private String versionName;
}

class File {
    @Id
    private Integer id;

    @Column(name="file_name")
    private String fileName;

    @ManyToOne
    @JoinTable(name="versioned_file",
       joinColumns= {
            @JoinColumn(name = "file_id")
       },
       inverseJoinColumns = {
            @JoinColumn(name = "version_id")
       }
    )
    private Version version;
}


Code between sessionFactory.openSession() and session.close():
Code:
          @Test
         public void testFindByVersions() {
               EntityManager em = fileDAO.getEntityManager();
               Query query = em.createQuery("from File where version.versionName in (:version1, :version2)");
               query.setParameter("version1", "3.6")
                    .setParameter("version2", "3.7");
               List<File> files = query.getResultList();
               assert null != files;
               for(File f : files) {
                   System.out.print(" fileId: "+f.getId());
                   System.out.print(", fileName: "+f.getFileName());
                   System.out.println(", fileVersion: "+ f.getVersion().getVersionName());
               }
         }


Name and version of the database you are using:
Sybase ASE 12.5.3

The generated SQL (show_sql=true):
Code:
select
    file0_.id as id0_,
    file0_.fileName as fileName0_,
    file0_1_.version_id as version1_1_ ,
    version1_.versionName
from
    common.dbo.file file0_
left outer join
    common.dbo.file_version file0_1_
        on file0_.id=file0_1_.file_id,
    common.dbo.version version1_
where
    file0_1_.version_id=version1_.id
    and (
        version1_.versionName in (
            ?, ?
        )
    )


I am trying to map a unidirectional Many-to-Many relation from child to parent -- kind of like this except in reverse:
http://www.hibernate.org/hib_docs/refer ... l-join-m2m

The idea being that a given file has a single version associated with it (even though multiple versions may contain the same file).

The SQL generated by Hibernate (above) will print out a repeating file associated with different versions, but I cannot uncover a way to map it without being required to have a collection of versions in the above File class -- which does not make sense from the perspective of the domain model & use cases.

The database query above returns this:
Code:
id0_  fileName0_         version1_1_ versionName
1      core.exe          3               3.6
2      framework.dll     3               3.6
2      framework.dll     4               3.7


But after being mapped to the domain model the above Test case prints out this:
fileId: 1, fileName: core.exe, fileVersion: 3.6
fileId: 2, fileName: framework.dll, fileVersion: 3.6
fileId: 2, fileName: framework.dll, fileVersion: 3.6

Note: the fileVersion is the same for files mapped to different versions.

I don't understand why hibernate would associate a different row of the data mapped via the join table than the result provides?

Is there a way to accomplish mapping this to the domain model we expect?

Thanks,
Ed


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.