-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping interfaces
PostPosted: Sun Jul 06, 2008 8:08 pm 
Newbie

Joined: Wed May 21, 2008 11:21 am
Posts: 11
Hi guys,
I know this must be a very common situation, but surprisingly i can't find a clear explanation or sample. I'm new to Hibernate and Java in general so if you can please help me to get started I would appreciate it.

Basically this is about mapping an interface rather than a class because I have interfaces for all my business objects. So, I have the User interface
Code:
public interface User extends Entity {
        public Long getId();
        //set id should be private
   public String getUsername();
   public void setUsername( String username );   
}

and the UserImpl class
Code:
public class UserImpl implements User {
        private long id;
   private String username;
        public long getId(){return this.id}
        private void setId(long id){this.id=id;}
   public String getUsername() {return username;}
   public void setUsername(String username) {this.username = username;   }
}

I use the interface through the code. Even in my UserDao class.
Code:
List<User> usrs = this.getHibernateTemplate().find("from User user where user.username=?",username);

The documentation says that I can map an interface like a class so I wrote this User.hbm.xml
Code:
<hibernate-mapping>
     <class  name="User"   table="users">
      <id name="id"><generator class="native"></generator></id>
      <property    name="username" not-null="true" length="32"/>
   </class>   
</hibernate-mapping>

All my tries from this startup setup failed. First of all it says that it cannot find a setter for the id field in the User class (so it sees it as class not interface). I would like to keep that setter private so that's one problem. Even though I make the setter public i get all sort of other problems.

What is the standard way to tackle this situation? Any advice, no matter how small is highly appreciated. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 09, 2008 2:01 pm 
Newbie

Joined: Wed May 21, 2008 11:21 am
Posts: 11
I get it now. Hibernate can't know how the field is called in the implementing class (i tried to set the access="field" in the mapping xml). That's why i must provide a setter and getter method for all fields mapped to the interface.
No problem, i can provide a wrapping class that hides the required methods when i want to.


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