-->
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.  [ 5 posts ] 
Author Message
 Post subject: ID of many-to-one relation
PostPosted: Sun Jan 04, 2004 4:18 pm 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
Is there any way of retrieving a foreign key in a table without hitting the database for the table the foreign key relates to?

ie

Code:
<hibernate-mapping>
   
    <class
        name="eg.BlogItem"
        table="BLOG_ITEMS"
        dynamic-update="true"
        lazy="true">
       
        <id
            name="id"
            column="BLOG_ITEM_ID">
           
            <generator class="native"/>
           
        </id>
       
        <property
            name="title"
            column="TITLE"
            not-null="true"/>
           
        <property
            name="text"
            column="TEXT"
            not-null="true"/>
           
        <property
            name="datetime"
            column="DATE_TIME"
            not-null="true"/>
           
        <many-to-one
            name="blog"
            column="BLOG_ID"
            not-null="true"/>
           
    </class>
   
</hibernate-mapping>


with class definition

Code:
package eg;

import java.text.DateFormat;
import java.util.Calendar;

public class BlogItem {
    private Long _id;
    private Calendar _datetime;
    private String _text;
    private String _title;
    private Blog _blog;

    public Blog getBlog() {
        return _blog;
    }
    public Calendar getDatetime() {
        return _datetime;
    }
    public Long getId() {
        return _id;
    }
    public String getText() {
        return _text;
    }
    public String getTitle() {
        return _title;
    }
    public void setBlog(Blog blog) {
        _blog = blog;
    }
    public void setDatetime(Calendar calendar) {
        _datetime = calendar;
    }
    public void setId(Long long1) {
        _id = long1;
    }
    public void setText(String string) {
        _text = string;
    }
    public void setTitle(String string) {
        _title = string;
    }
}


I would just like to get the actual content of the Blog column from a BlogItem rather than going BlogItem.getBlog().getId() which will persist Blog from the database which is unecessary as I only want the id which will be the foreign key in the BlogItem table. Assume lazy initialisation is being used.

Cheers.

Myk.


Top
 Profile  
 
 Post subject: Re: ID of many-to-one relation
PostPosted: Sun Jan 04, 2004 4:46 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Either
Code:
<property name="blogId" column="BLOG_ID" insert="false" update="false"

or proxy the Blog class. Id return won't load blog from db
Code:
<class
        name="eg.Blog"
        table="BLOG" lazy="true">

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 4:58 pm 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
Thanks for the quick reply :)

The second solution is what I am after.

Just curious but does the first solution now have two "blog id" columns, one created by that property, and the other implicitly by the many-to-one relation tag?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 5:08 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Nope, same column name prevent duplication and insert and update = false means this is a read only property : no concurrent modification from 2 java properties. Actually, if you don't set the 2 attributes to false, Hibernate will consider the mapping false.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 7:36 pm 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
Gotcha.

Thanks again.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.