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.  [ 7 posts ] 
Author Message
 Post subject: creating a new non-mapped field in an object
PostPosted: Mon Jul 07, 2008 7:53 am 
Newbie

Joined: Mon Jul 07, 2008 7:14 am
Posts: 3
Hi there

I'm working on a hibernate application of someone else's design, and this is the first time I've used this technology!

The application maps to a series of database tables, and represents them as java objects with realtionships between them that mirror the database itself. This I understand. What I'm trying to do now is to establish a relationship between these objects that is not explicitily defined in the database - a parent/child relationship - which I have to work it out from the data.

So, I'd like to create a new field in the main class (Name) that stores an instance of the class itself.

My attempt to do this was pretty straightforward, I simply declared a new field with getters and setters, like so:
Code:
   private Name parentName;

   public void setParentName(Name parentName) {
      this.parentName= parentName;
   }
   
   public Name getParentName() {
      return parentName;
   }


When I run this however, the program falls over, with the compiler saying it can't determine a type for this object. So instead of the object I tried to create a simple String variable instead. In this case it objected because the variable wasn't mapped to a field in the database. But that's not what I want to do in this case - I want to create a new variable that I populate on the fly.

So can anyone tell me what I'm doing wrong? I presume it's possible to include non-mapped variables in my objects. How can I convince hibernate not to worry that there isn't a field in the database mapped to this one?

thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 9:22 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, if you mark an attribute as transient, then Hibernate won't try to manage the persistence lifecycle of that property. That's probably what you need.

http://jpa.ezhibernate.com/Javacode/lea ... ingwithjpa

Quote:
Non-Persistent JavaBean Properties

It's not unreasonable to expect that from time to time, a JavaBean will contain some properties that need to be persisted to a database, and some properties that do not need to be stored in the database. However, when you mark a POJO with the @Entity annotation, Hibernate will attempt to persist every property of the JavaBean. The default behavior of Hibernate is to persist every non-transient and non-static property of a POJO.

However, if your JavaBean has an instance level property that you do not want Hibernate to save to the database, you can simply decorate the non-persistent property with a simple little @Transient annotation.
Transient Annotation JavaDoc:
Annotation Type Transient

________________________________________

@Target(value={METHOD,FIELD})
@Retention(value=RUNTIME)

public @interface Transient

This annotation specifies that the property or field is not persistent. It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class.

________________________________________
Using the @Transient Annotation

package com.examscam.hib.exam;
import javax.persistence.Column;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

@Entity
@Table (name="user", schema="examscam")
public class User {
private int id;
private String loginName;
private String password;


private String encryptedPassword;

@Transient
public String getEncryptedPassword () {
return encryptedPassword;
}
public void setEncryptedPassword(String ep){
this. encryptedPassword = ep;
}

@Id
@GeneratedValue
@Column (name="id")
public int getId() {return id;}
public void setId(int id) {this.id = id;}
@Column (name="login_name")
public String getLoginName() {return loginName;}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
@Column (name="password")
public String getPassword() {return password;}
public void setPassword(String password) {
this.password = password;
}
public static void main(String args[]){ }
}


http://jpa.ezhibernate.com/Javacode/lea ... ingwithjpa

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 9:58 am 
Newbie

Joined: Mon Jul 07, 2008 7:14 am
Posts: 3
Thanks for your reply Cameron, looks like a potential solution to me. However I've not had success with it so far! I attempted to mark the column as transient, as you suggested:

Code:
import javax.persistence.Transient;
... etc etc ...

private String parentName;

@Transient
public String getParentName() {
   return parentName;
}
public void setParentName(String pName) {
   this.parentName= pName;
}


Again though, same error. Hibernate still seems to be looking for the parentName column in the database...

Code:
[java] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'checklistSessionFactory' defined in file [C:\www\HibProject\build\applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing column: parentName in hib_db.dbo.Name
[java] Caused by: org.hibernate.HibernateException: Missing column: parentName in hib_db.dbo.Name


I've read around the article you sent and as far as I can tell I've got all the right code in to declare an element as transient. Hmm what am I missing? Incidentally, I never write back to the database in any case, this is a read-only write-to-file application.

cheers


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 4:24 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Well, I'm not sure of the
Code:
@Transient
annotation works if placed upon a getter method.

I've some entities where I use this transient beasts - but I always put the annotation upon the instance variables, not on the methods.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 10:19 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Certainly the trend it to put the annotation on the fields, as opposed to the getter, but so long as your consistent, it will work. Don't mix and match though.

That was the direction on the last couple of projects I was on. Placing the Hibernate Annotation on the getter has now become a bit of a habit.

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 08, 2008 6:31 am 
Newbie

Joined: Mon Jul 07, 2008 7:14 am
Posts: 3
Nice one, @Transient on the variable seems to work. I'll post you guys some credits once I can figure out how...!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 08, 2008 7:44 am 
Newbie

Joined: Tue Jul 08, 2008 6:43 am
Posts: 1
"rate post" at the bottom of each message.


davemere wrote:
Nice one, @Transient on the variable seems to work. I'll post you guys some credits once I can figure out how...!


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