The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

KayDee (2) [Avatar] Offline
#1
Table-per-subclass mapping issue:

How do I persist the subclass of a class using table-per-subclass mapping?
Using hibernate, I created the following tables and mappings.

I inserted the following data into the events table:
(1, “Filmshow”, 5)

I tried to do the following but it failed with the following error:
not-null property references a null or transient value: Event.name;

public class Main {

public Main() {
super();
}

public static void main(String[] args) {
Configuration cfg = new Configuration()
.addClass(NetworkingEvent.class)
.setProperty("hibernate.dialect", org.hibernate.dialect.MySQLInnoDBDialect")
.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/javalobby")
.setProperty("hibernate.connection.username", "admin");

SessionFactory sessions = cfg.buildSessionFactory();

Session session = sessions.openSession();
try {
NetworkingEvent netEvent = new NetworkingEvent();
netEvent.setId(1);
netEvent.setNumSeats(5);
session.saveOrUpdate(netEvent);
System.out.println(user);
}
finally {
session.close();
}

}
}

The parent class:
----------------------------------
/**
* @hibernate.class
* table="event "
*/
public class Event implements Serializable {
private long id;
private String name;
Private int duration;
public Event () {
}
/**
* @hibernate.id
* generator-class="assigned"
* type="long"
* column="id"
*/
public long getId() {
return id;
}
public void setId(long theId) {
id = theId;
}
/**
* @hibernate.property length="50" not-null="true" unique="true"
*/
public String getName() {
return name;
}
public void setName(String name) {
name = name;
}

//Other properties
}
----------------------------------
The child class:
----------------------------------
import java.io.Serializable;
/**
* @hibernate.joined-subclass
* table="net_Events"
*
* @hibernate.joined-subclass-key
* column="id"
*/
public class NetworkingEvent extends Event implements Serializable {
private int numSeats;
/** default constructor */
public NetworkingEvent() {
super();
}
/**
* @hibernate.property
* column="num_seats"
*/
public int getNumSeats() {
return numSeats;
}
public void setNumSeats (int numSeats) {
numSeats = numSeats;
}
}
The Event.hbm.xml file:
----------------------------------
<class name="Event" table="events">
<id name="id" type="long">
<generator class="native"/>
</id>
<joined-subclass name="NetworkingEvent" table="net_events">
<key column="id"/>
...
</joined-subclass>
</class>
ceracm (113) [Avatar] Offline
#2
Re: Table-per-subclass mapping issue
I quickly glanced at your code: setName() does not appear to be called.

Did I miss that - I haven't had coffee yet.

Chris
KayDee (2) [Avatar] Offline
#3
Re: Table-per-subclass mapping issue
No, it wasnt.

I thought, that wasnt necessary, that It could be retrieved from the parent table(Event) since the parent id=1, containing the information was provided.

Moreover, I tried doing that earlier on, ie. calling setName() but there was another error of

"Duplicate entry for key"