aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate Mapping Issue 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 Mapping Issue" Watch "Hibernate Mapping Issue" New topic
Author

Hibernate Mapping Issue

Luke Gee
Greenhorn

Joined: Apr 11, 2012
Posts: 5
Hi Guys,

Im a little stumped here and wondered if anyone may have an idea whats going on. It could just be something obvious that ive missed.
I have a table that references 2 others, but each record will only reference one or the other. The 2 hibernate mappings are almost identical, however only the occupation mapping is working. (note records are always null)

Here are the tables. There is a check constraint to make sure only one of them is not null for each record.



Here is the Java and mappings:

PersonRecord:




Occupation:




Note:




I was wondering if anyone would have any idea why one would work and the note object in PersonRecord will always be null. (the data is there in the db)

Many thanks for any help,

Luke
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
I think you mixed up two concepts, one-to-one association based on shared primary keys with one-to-one association based on foreign keys. You have to pick one or the other. Either share the personId as the primary key for both the occupationId and the noteId, in which case you do not generate separate primary keys for the Note and Occupation tables/objects, or use a foreign key association, in which case you do not put the noteId or occupationId fields themselves in the PersonRecord object, and map the association as a many-to-one with a unique constraint over the foreign-key column.

Check out the hibernate docs for more info and examples: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/associations.html#assoc-unidirectional-121
Saurabh Maheshwari
Greenhorn

Joined: Jan 09, 2013
Posts: 1
My problem is that
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
and Caused by: java.sql.BatchUpdateException: Field 'Person_Id' doesn't have a default value
and how to solve it.
pleasw let me know.

It is my person class

package com.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Person implements Serializable {

private static final long serialVersionUID = 4333499410830154895L;

private int id;
private String firstName = null;
private String lastName = null;

private Set<Address> addresses = new HashSet<Address>();

public int getId() {
return id;
}

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

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Set<Address> getAddresses() {
return addresses;
}

public void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}

public void addAddress(Address address) {
address.setPerson(this);
this.addresses.add(address);

}

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("**** Person **** ");
sb.append("Id: ");
sb.append(this.id);
sb.append("\n");
sb.append("First name: ");
sb.append(this.firstName);
sb.append("\n");
sb.append("Last name: ");
sb.append(this.lastName);
sb.append("\n");
sb.append("**** Person **** ");
sb.append("\n");

return sb.toString();
}

}


here my address class

package com.dto;

import java.io.Serializable;





public class Address implements Serializable {


private int id = 0;


private String firstLine = null;
private String secondLine = null;
private String city = null;
private String zipCode = null;
private Person person = null;
// private Integer personId = null;

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

public String getFirstLine() {
return firstLine;
}

public void setFirstLine(String firstLine) {
this.firstLine = firstLine;
}

public String getSecondLine() {
return secondLine;
}

public void setSecondLine(String secondLine) {
this.secondLine = secondLine;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getZipCode() {
return zipCode;
}

public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}

public Person getPerson() {
return person;
}

public void setPerson(Person person) {
this.person = person;
}

/* public Integer getPersonId() {
return personId;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("***** Address ******");
sb.append("\n");
sb.append("Id: ");
sb.append(this.id);
sb.append("\n");
sb.append("First Line: ");
sb.append(this.firstLine);
sb.append("\n");
sb.append("Second Line: ");
sb.append(this.secondLine);
sb.append("\n");
sb.append("City: ");
sb.append(this.city);
sb.append("\n");
sb.append("Zip Code: ");
sb.append(this.zipCode);
sb.append("\n");
sb.append("Person Id: ");
// sb.append(this.personId);
sb.append("\n");
sb.append("***** Address ******");
sb.append("\n");

return sb.toString();

}

}

here my Loader class


package com.dto;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.dto.HibernateUtil;
public class DataLoader {

public static void main(String[] args) {

SessionFactory factory = HibernateUtil.getSessionFactory();

Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Person p = new Person();
// p.setId(1);

p.setFirstName("Bill");
p.setLastName("Banks");

Address a1 = new Address();
a1.setFirstLine("Line 11");
a1.setSecondLine("Line 12");
a1.setZipCode("12345");
//a1.setId(1);
a1.setPerson(p);
p.addAddress(a1);

Address a2 = new Address();
a2.setFirstLine("Line 21");
a2.setSecondLine("Line 22");
a2.setZipCode("54321");
//a2.setId(2);
a2.setPerson(p);
p.addAddress(a2);

Address a3 = new Address();
a3.setFirstLine("Line 31");
a3.setSecondLine("Line 32");
a3.setZipCode("11221");
//a3.setId(3);
a3.setPerson(p);
p.addAddress(a3);

Address a4 = new Address();
a4.setFirstLine("Line 41");
a4.setSecondLine("Line 42");
a4.setZipCode("22331");
//a4.setId(4);
a4.setPerson(p);
p.addAddress(a4);

Person p1 = new Person();
//p1.setId(2);
p1.setFirstName("Steve");
p1.setLastName("Jobs");

Address a11 = new Address();
a11.setFirstLine("Line 111");
a11.setSecondLine("Line 112");
a11.setZipCode("12345");
//a11.setId(5);
a11.setPerson(p1);
p1.addAddress(a11);

Address a22 = new Address();
a22.setFirstLine("Line 221");
a22.setSecondLine("Line 222");
a22.setZipCode("54321");
//a22.setId(6);
a22.setPerson(p1);
p1.addAddress(a22);

Address a33 = new Address();
a33.setFirstLine("Line 331");
a33.setSecondLine("Line 332");
a33.setZipCode("11221");
//a33.setId(7);
a33.setPerson(p1);
p1.addAddress(a33);

Address a44 = new Address();
a44.setFirstLine("Line 441");
a44.setSecondLine("Line 442");
a44.setZipCode("22331");
//a44.setId(8);
a44.setPerson(p1);
p1.addAddress(a44);

Person p2 = new Person();
//p2.setId(3);
p2.setFirstName("Clark");
p2.setLastName("Jacob");

Address a111 = new Address();
a111.setFirstLine("Line 111");
a111.setSecondLine("Line 112");
a111.setZipCode("12345");
//a111.setId(9);
a111.setPerson(p2);
p2.addAddress(a111);

Address a222 = new Address();
a222.setFirstLine("Line 221");
a222.setSecondLine("Line 222");
a222.setZipCode("54321");
a222.setId(10);
a222.setPerson(p2);
p2.addAddress(a222);

session.save(p);
session.save(p1);
session.save(p2);

tx.commit();
}

}

here my Person.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.dto">
<class name="Person" table="Person">
<id name="id" column="Person_Id">
<generator class="increment"></generator>
</id>
<property name="firstName" column="First_Name"></property>
<property name="lastName" column="Last_Name"></property>
<set name="addresses" cascade="all" inverse="true" fetch="select">
<key column="Person_Id" not-null="true"></key>
<one-to-many class="Address"/>
</set>
</class>
</hibernate-mapping>


here my Address.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.dto">
<class name="Address" table="Address">
<id name="id" column="Adress_Id">
<generator class="increment"></generator>
</id>
<property name="firstLine" column="First_Line"></property>
<property name="secondLine" column="Second_Line"></property>
<property name="city" column="City"></property>
<property name="zipCode" column="Zip_Code"></property>

<many-to-one name="person" class="Person" cascade="all">

</many-to-one>
</class>
</hibernate-mapping>

My problem is here

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select max(Person_Id) from Person
Hibernate: select max(Adress_Id) from Address
Hibernate: insert into Person (First_Name, Last_Name, Person_Id) values (?, ?, ?)
Hibernate: insert into Address (First_Line, Second_Line, City, Zip_Code, person, Adress_Id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Address (First_Line, Second_Line, City, Zip_Code, person, Adress_Id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Address (First_Line, Second_Line, City, Zip_Code, person, Adress_Id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Address (First_Line, Second_Line, City, Zip_Code, person, Adress_Id) values (?, ?, ?, ?, ?, ?)
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:91)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:86)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:171)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2048)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2427)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.dto.DataLoader.main(DataLoader.java:114)
Caused by: java.sql.BatchUpdateException: Field 'Person_Id' doesn't have a default value
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1269)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:955)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 15 more

 
 
subject: Hibernate Mapping Issue