-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Table-per-subclass mapping issue:
PostPosted: Fri Jul 14, 2006 8:49 am 
Newbie

Joined: Fri Jul 14, 2006 8:42 am
Posts: 2
Hibernate version:3.0


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>


Full stack trace of any exception that occurs:
not-null property references a null or transient value: Event.name;


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 12:12 pm 
Regular
Regular

Joined: Wed Jan 11, 2006 12:49 pm
Posts: 64
Location: Campinas, Brazil
The error said exactly what the problem really was:
Quote:
/**
* @hibernate.property length="50" not-null="true" unique="true"
*/
public String getName() {
return name;
}

versus
Quote:
NetworkingEvent netEvent = new NetworkingEvent();
netEvent.setId(1);
netEvent.setNumSeats(5);
session.saveOrUpdate(netEvent);

not-null property references a null or transient value: Event.name; means that property name of class Event should not be null, but it is.

_________________
Henrique Sousa
Don't forget to rate useful responses


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 14, 2006 12:36 pm 
Newbie

Joined: Fri Jul 14, 2006 8:42 am
Posts: 2
That was what I thought too.

But on calling setName() there was another error of "Duplicate entry for key".


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.