-->
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.  [ 9 posts ] 
Author Message
 Post subject: Mapping an object without primary key
PostPosted: Wed Feb 16, 2005 11:00 am 
Newbie

Joined: Fri Feb 27, 2004 6:49 pm
Posts: 18
Location: Paris, France
Hibernate version: 2.1.7

Hello,

I'm trying to map an oracle view to a persistent java object. So there is no real primary key. I still have a functionnal one I can use but I would prefer not to since I don't need it (an also, there would be a component in it which is not accepted in a composite id so I would need to do a user type also).

Hibernate documentation (section 4.1.3 ) seems to tell that a primary key is not mandatory. However, the DTD doesn't seem to agree. An id tag is mandatory as well as a generator. Then, section 5.1.4 is telling us that

Quote:
If the name attribute is missing, it is assumed that the class has no identifier property.


The problem is that when name is missing, it seems to be defaulted to "id". So it still doesn't work.

I also had a look at the wiki in the advanced problems section that seems to tell a primary key is in fact needed. In this case the documentation is out-of-date.

Can someone help me?

Thanks,
Henri

P.S.: I also thought about a composite-id without properties by the DTD doesn't like that either.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 11:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
A primary key is mandatory, an id-property is not (although without an id property main features can not be used)


Top
 Profile  
 
 Post subject: Re: Mapping an object without primary key
PostPosted: Wed Feb 16, 2005 11:39 am 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
You're gonna need some way of distinguishing unique rows/objects. Sounds like composite id is the way to go. If the rows are not identifiably unique, you have fundamental problems with your DB design which Hibernate cannot fix.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 12:26 pm 
Newbie

Joined: Wed Sep 29, 2004 10:10 am
Posts: 13
Location: Washington D.C. USA
I found out that Hibernate needs a primary key other wise it thinks that it is not a relational entity.

In your view can you define a field called 'view_id' which is a Oracle sequence ? and you can define view_id as a primary key.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 12:36 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
rmahajan wrote:
I found out that Hibernate needs a primary key other wise it thinks that it is not a relational entity.

In your view can you define a field called 'view_id' which is a Oracle sequence ? and you can define view_id as a primary key.


That would kind of hack around the problem, obscuring the real issue. Either a composite id will work, or his design is wrong.

If there is no unique composite id, then Hibernate is right: it is not a relational entity. Tuples must be unique. Redesign the view so that a unique composite id is available for each row, and use that. Don't fake it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 1:27 pm 
Newbie

Joined: Fri Feb 27, 2004 6:49 pm
Posts: 18
Location: Paris, France
What do you mean by id-property? You mean that I don't need a primary key in the DB but I do need an id in the mapping?

About my design:
I have a primary key (kind of) on my object but I don't really care about it. The object is not mutable. And won't be loaded twice. And I would have like the key to be a component.

But anyway, I've found a not so bad workaround. I've put a composite-id and a getter creating my component from the composite-id property.

Still, I think the documentation should be modified in the two sections I mentioned because they clearly refer to an optional identifier property.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 1:57 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
henri wrote:
What do you mean by id-property? You mean that I don't need a primary key in the DB but I do need an id in the mapping?

You absolutely need an identifier in the mapping (describing how it is unique: by PK, assigned, or composite), but you don't need an id field in the Java class, or getId() and setId(). Without these, though, Hibernate cannot saveOrUpdate(), or cascade id changes to associations, since both require access to the id by Hibernate.
Again, your rows must be unique to be relational. This is Set theory after all. If they are, there is no problem.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 5:20 pm 
Newbie

Joined: Fri Feb 27, 2004 6:49 pm
Posts: 18
Location: Paris, France
Cool, they are unique (equals and hashCode are overloaded). And since I don't need saveOrUpdate, no problem there.

If I need an id what should it looks like? Mmm, I think I just guessed correctly.

Code:
      <id type="long">
         <generator class="assigned"/>
      </id>


Of course, every information here is dummy but it seems to be loaded correctly. (I'm not in a situation where I can test the querying right now)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 5:40 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
Again, I highly highly recommend composite-id *instead* of id. Your case is why it is there. Try getting component composite id to work again.

http://www.hibernate.org/hib_docs/reference/en/html/components.html#components-compositeid

the example:
Code:
<class name="eg.Foo" table"FOOS">
    <composite-id name="compId" class="eg.FooCompositeID">
        <key-property name="string"/>
        <key-property name="short"/>
        <key-property name="date" column="date_" type="date"/>
    </composite-id>
    <property name="name"/>
    ....
</class>


This will work best for you. Again, your *database* rows must be unique to implement this. If they are not, you need to redesign anyway. If you overrode equals() using business properties, than these are good candidates for composite-id (and a component). If you overrode it some other way (using object identity), then read the docs again on this issue.


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

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.