I have been learning about hibernate the last few days and had some general question. I started learning how to map the relationships in my project using xml mapping; now I am trying to go back through my project and see if I can implement annotations. I seem to keep running into the same question, in order for an aggregated relationship to get mapped properly I keep having to make an instance of the classes I aggregated from in the classes I am aggregating too, lol, that seems so weird… Putting an instance of the class, you aggregated from, into the class you aggregated too… Is that like an infinite loop? Like yesterday, I was trying to build this xml mapping, with annotation.
class user...
private int userId
private string username
private string password
Private Person person
Class person...
private int personId;
private String firstName;
private String lastName;
private String email;
Code:
<hibernate-mapping package="my.bank.BLL.objects">
<class name="User" table="USER" dynamic-update="true">
<id name="userId" type="int" column="USER_ID">
<generator class="native"/>
</id>
<property name="username" type="string">
<column name="USERNAME" length="45" not-null="true" />
</property>
<property name="password" type="string">
<column name="PASSWORD" length="45" not-null="true"/>
</property>
<one-to-one name="person" class="my.bank.BLL.objects.Person"
cascade="save-update" lazy="false">
</one-to-one>
</class>
</hibernate-mapping>
And this worked perfectly, I did not have to reference anything special in my persons xml mapping, just map the columns. Well.. I did have to make sure the personId matched the UserID, but that wasn't hard.
And yesterday I couldn't believe how much I struggled just to get annotations to do this same thing...
Code:
@Entity
@Table(name = "user")
public class User implements Serializable {
private int userId;
private String username;
private String password;
private Person person;
public User() {
}
@Id
@GeneratedValue
@Column(name = "USER_ID")
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@Column(name = "USERNAME", nullable = false, length = 45)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "PASSWORD", nullable = false, length = 45)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
And the person...
Code:
@Entity
@Table(name = "person")
public class Person implements Serializable {
private int personId;
private String firstName;
private String lastName;
private String email;
private User user; <------------------------------
public Person() {
}
@Id
@GeneratedValue
@Column(name = "PERSON_ID")
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
@Column(name = "FIRST_NAME", nullable = false, length = 45)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "LAST_NAME", nullable = false, length = 45)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name = "EMAIL", nullable = false, length = 45)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@OneToOne
@PrimaryKeyJoinColumn
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
With the annotation is when I have to start referencing my aggregated object inside the object I aggregated from, which confuses me.