aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate 4.1 : One-to-One mapping association “broken column mapping for” error using XML mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate 4.1 : One-to-One mapping association “broken column mapping for” error using XML mapping" Watch "Hibernate 4.1 : One-to-One mapping association “broken column mapping for” error using XML mapping" New topic
Author

Hibernate 4.1 : One-to-One mapping association “broken column mapping for” error using XML mapping

Kim Ming Yap
Ranch Hand

Joined: Dec 17, 2008
Posts: 53

I'm having issue doing a one-to-one mapping association where i get the 'broken column mapping for' error.

Exception in thread "main" org.hibernate.MappingException: broken column mapping for: department.id of: org.tutorial.hibernate.xml.dto.UserXML

Would appreciate any help.
Thanks.

Here are my code:

a. Class UserXML

public class UserXML {

private SocialInsuranceXML sin;
private Date dateJoined;
private String remarks;
private AddressXML homeAddress;
private AddressXML mailingAddress;
private Collection<AddressXML> addressHistory = new ArrayList<AddressXML>();
private DepartmentXML department;

public SocialInsuranceXML getSin() {
return sin;
}

public void setSin(SocialInsuranceXML sin) {
this.sin = sin;
}

public Date getDateJoined() {
return dateJoined;
}

public void setDateJoined(Date dateJoined) {
this.dateJoined = dateJoined;
}

public String getRemarks() {
return remarks;
}

public void setRemarks(String remarks) {
this.remarks = remarks;
}

public AddressXML getHomeAddress() {
return homeAddress;
}

public void setHomeAddress(AddressXML homeAddress) {
this.homeAddress = homeAddress;
}

public AddressXML getMailingAddress() {
return mailingAddress;
}

public void setMailingAddress(AddressXML mailingAddress) {
this.mailingAddress = mailingAddress;
}

public Collection<AddressXML> getAddressHistory() {
return addressHistory;
}

public void setAddressHistory(Collection<AddressXML> addressHistory) {
this.addressHistory = addressHistory;
}

public DepartmentXML getDepartment() {
return department;
}

public void setDepartment(DepartmentXML department) {
this.department = department;
}

@Override
public String toString() {
return "UserXML [sin=" + sin + ", dateJoined=" + dateJoined + ", remarks=" + remarks + ", homeAddress=" + homeAddress
+ ", mailingAddress=" + mailingAddress + ", addressHistory=" + addressHistory + ", department=" + department + "]";
}

}

b. Class DepartmentXML

public class DepartmentXML {

private int id;
private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "department [id=" + id + ", name=" + name + "]";
}

}

c. userxml.hbm.xml

<hibernate-mapping package="org.tutorial.hibernate.xml.dto">
<class name="UserXML" table="user_xml">
<composite-id name="sin" class="org.tutorial.hibernate.xml.dto.SocialInsuranceXML">
<key-property name="id" column="sin_id" />
<key-property name="name" column="user_name" />
</composite-id>
<property name="dateJoined" column="date_joined" type="date" />
<property name="remarks" column="remarks" type="text" />
<component name="homeAddress">
<property name="street" column="home_street_name" />
<property name="city" column="home_city_name" />
<property name="state" column="home_state_name" />
<property name="zipCode" column="home_zipcode_name" />
</component>
<component name="mailingAddress">
<property name="street" column="mailing_street_name" />
<property name="city" column="mailing_city_name" />
<property name="state" column="mailing_state_name" />
<property name="zipCode" column="mailing_zipcode_name" />
</component>
<idbag name="addressHistory" table="address_history_xml">
<collection-id type="long" column="address_id">
<generator class="hilo" />
</collection-id>
<key>
<column name="sin_id" />
<column name="user_name" />
</key>
<composite-element class="org.tutorial.hibernate.xml.dto.AddressXML">
<property name="street" column="street_name" />
<property name="city" column="city_name" />
<property name="state" column="state_name" />
<property name="zipCode" column="zipcode_name" />
</composite-element>
</idbag>
<one-to-one name="department" class="org.tutorial.hibernate.xml.dto.DepartmentXML" />
</class>
</hibernate-mapping>

d. departmentxml.hbm.xml

<hibernate-mapping package="org.tutorial.hibernate.xml.dto">
<class name="DepartmentXML" table="department_xml">
<id name="id" column="department_id" />
<property name="name" column="department_name" />
</class>
</hibernate-mapping>
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1024
    
    5

I think your relationship is actually many to one, not one to one i.e. many users will belong to one department.

So, in your userxml.hbm.xml file, change the relation to:


As a side issue, consider dropping the XML suffix from your class names. It doesn't follow standards and obscures their meaning. For example, your UserXML represents a User, your DepartmentXML a department.
Kim Ming Yap
Ranch Hand

Joined: Dec 17, 2008
Posts: 53

It's a one-to-one unidirectional - not bidirectional.
I'm just curious why won't it work?
The annotated form did work as shown below. It ran successfully.

User.java

@Entity(name = "User")
@Table(name = "user")
public class User {

@EmbeddedId
private SocialInsurance sin;

@NotFound(action = NotFoundAction.IGNORE)
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "department_id")
private Department department;
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1024
    
    5

When using annotations, what does the Department class look like?
Kim Ming Yap
Ranch Hand

Joined: Dec 17, 2008
Posts: 53

Here it is.

@Entity(name = "Department")
@Table(name = "department")
public class Department {

@Id
@Column(name = "department_id")
private int id;

@Column(name = "department_name")
private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "department [id=" + id + ", name=" + name + "]";
}

}
 
wood burning stoves
 
subject: Hibernate 4.1 : One-to-One mapping association “broken column mapping for” error using XML mapping