i want to write a simple many-to-one ORM using hibernate. I have tow tables, user and location. Location contains country names one of which i want to refer in user class.
public class User extends BusinessBaseImpl implements UserInterface{
private String id;
private String firstName;
private String lastName;
private String mobileNumber;
private Location location;
private String email;
private String password;
private String type;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
makeDirty();
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
makeDirty();
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
makeDirty();
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
makeDirty();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
makeDirty();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
makeDirty();
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
makeDirty();
}
public String getId() {
return id;
}
@SuppressWarnings("unused")
private void setId(String id){
this.id = id;
}
/**
*
* Private constructor.
*/
public User(){
this.id = CoreService.getUID();
this.isNew = true;
this.isDirty = false;
}
}
hibernate configuration file
<hibernate-mapping>
<class name="pspl.assignment.businessimpl.User" table="user">
<id name="id" type="string" column="id">
<ge开发者_如何学编程nerator class="assigned"></generator>
</id>
<property name="firstName">
<column name="firstname" />
</property>
<property name="lastName">
<column name="lastname" />
</property>
<property name="email">
<column name="email" />
</property>
<property name="mobileNumber">
<column name="mobilenumber" />
</property>
<many-to-one name="location" class="pspl.assignment.businessimpl.Location" column="location" >
</many-to-one>
<property name="password">
<column name="password" />
</property>
<property name="type">
<column name="type" />
</property>
</class>
The location class
public class Location extends BusinessBaseImpl {
private String id;
private String contryName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getContryName() {
return contryName;
}
public void setContryName(String contryName) {
this.contryName = contryName;
}
public Location(){
this.setId(CoreService.getUID());
this.isDirty = false;
this.isNew = true;
}
public static Location createNew(){
return new Location();
}
}
When iam exicuting a test case on the user class,i am getting following exception
org.hibernate.MappingException: Could not determine type for: pspl.assignment.businessimpl.Location, at table: user, for columns: [org.hibernate.mapping.Column(location)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:269)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
at org.hibernate.mapping.Property.isValid(Property.java:185)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:440)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)..............
Mapping file for locaion
<hibernate-mapping>
<class name="pspl.assignment.businessimpl.Location" table="location">
<id name="id" type="string" column="id"></id>
<property name="contryName"><column name="contryname"/> </property>
</class>
Hibernate configuration file
<hibernate-configuration>
<mapping resource="location.hbm.xml"/>
<mapping resource="user.hbm.xml"/>
I am new to hibernate, and i dont know where i am doing mistake.
Thanks
精彩评论