• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When using annotations, what does the Department class look like?
 
Kim Ming Yap
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 + "]";
}

}
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic