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!