-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping to simple two-column-tables (id, natural-id)?
PostPosted: Mon Jun 09, 2008 10:32 am 
Newbie

Joined: Tue Dec 05, 2006 8:36 am
Posts: 7
I'll dive right into a simple example to explain my question:

Code:
table log_entry(id, level, msg)
table log_level(id, name)


log_entry.level references log_level.id
a unique constraint on log_level.name

Code:
select * from log_level
1, ERROR
2, INFO
3, DEBUG

("ERROR" etc. acts here as a natural id, such that the row of log_level is identified in a unique way)

Now for the Hibernate Mapping for the log_entry:

I could just introduce a property for the level, which would yield something like a setLevel(int) method, by simply setting the foreign key.
Or I could use a many-to-one mapping, that would result in a setLevel(LogLevel) method, but this way I would have to use a LogLevel object.

But is it possible to create a mapping for the log_entry, such that you cat set the log level of a log entry by using e.g. setLevel("ERROR")?

I've looked into several mapping chapters but have not found anything with which I could realize the mapping above. Is it possible to realize with Hibernat at all?
Code:


Top
 Profile  
 
 Post subject: re:Mapping to simple two-column-tables (id, natural-id)?
PostPosted: Mon Jun 09, 2008 11:13 am 
Beginner
Beginner

Joined: Fri Jun 29, 2007 11:12 am
Posts: 25
Hello,
pick option 2 (manyToOne), you can do something like this:

Code:
...
LogLevel level;

setLogLevel(String level) {
level = new LogLevel(level);
}
...


make sure to use field access instead of property.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 09, 2008 12:26 pm 
Newbie

Joined: Tue Dec 05, 2006 8:36 am
Posts: 7
Does this really solve the problem? The setLogLevel("ERROR") will create a new LogLevel, but when persisting, hibernate doesn't know the primary key (in the example above: 1). The missing step here is to look for the primary key 1 with the (unique) name "ERROR" before persisting.


Top
 Profile  
 
 Post subject: re:Mapping to simple two-column-tables (id, natural-id)?
PostPosted: Mon Jun 09, 2008 5:31 pm 
Beginner
Beginner

Joined: Fri Jun 29, 2007 11:12 am
Posts: 25
Hello,
The only other option i can suggest is using the actual LogLevel object, with any access type you prefer, and the logic can be on the DAO layer. Here is a sample:

Code:
...
public class LogEntryDAOHibernate implements LogEntryDAO {
...

   public LogEntry setLogLevel(LogEntry entry, String level) {
       // get existing level
       Query q = session.createQuery("from LogLevel lev +
                                                      where lev.name = :name")
                                  q.setParameter("name", level);
       LogLevel logLevel = (LogLevel) q.uniqueResult();
       entry.setLogLevel(logLevel);
       session.saveOrUpdate(entry);
       ...
   }
}


you hit the database twice in this case.


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