-->
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.  [ 7 posts ] 
Author Message
 Post subject: inheritance problem does not allow NULLs
PostPosted: Tue Mar 07, 2006 8:19 am 
Newbie

Joined: Tue Mar 07, 2006 7:20 am
Posts: 16
I have big trouble setting up an inheritance relation for use with J2SE/EJB3.

I have created a Person and an Employee extending the person.
The Person has an auto filled in ID and the Employee inherits that. I have not yet found any examples that cover this.

There are probably better ways to do this (without inheritance), but I'm looking for the limits and want to know how inheritance is supported by Hibernate EJB3.

Person
Code:
package domain;

import java.util.Date;
import javax.persistence.*;

/**
*
*/
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Person
{
    protected Long id;
    protected String name;
    protected String firstname;
    protected String street;
    protected String city;
    protected String country;

    @Temporal(TemporalType.DATE)
    protected Date birth;

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    public Long getId()
    {
        return id;
    }
//and more get() and set()-ers


Employee
Code:
package domain;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;

@Entity
public class Employee extends Person {
   
   protected Department department;
   
   
    @ManyToOne
    public Department getDepartment()
    {
      return department;
     }

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



Hibernate version:
Hibernate 3.1.2
Entitymanager 3.1beta6
Annotations 3.1beta8
Mapping documents:

Code between sessionFactory.openSession() and session.close():

Code:
emf = Persistence.createEntityManagerFactory("manager1", new HashMap())
emf.createEntityManager();
EntityTransaction tx = emf.getTransaction();                       
tx.begin();
       
        Department dep = new Department();
        dep.setName("Boekhouding");
       
        Employee pietje = new Employee();
        Calendar cal = Calendar.getInstance();
        cal.set(1980, 12, 5);
        pietje.setBirth(cal.getTime());
        pietje.setFirstname("Piet");
        pietje.setName("Vennema");
        pietje.setCity("Veenendaal");
        pietje.setStreet("Dorpsstraat 62");
        pietje.setCountry("NL");
       
        //department
        pietje.setDepartment(dep);
        dep.addEmployeeToPayroll(pietje);
               
        em.persist(pietje);
       
tx.commit();


Full stack trace of any exception that occurs:
java.sql.SQLException: Cannot insert the value NULL into column 'PERSON_ID', table 'test.test.Employee'; column does not allow nulls. INSERT fails.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:365)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2781)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2224)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:628)
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:525)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:487)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:421)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2065)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2407)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:642)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:616)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:620)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:127)
at EJB3Test.go(EJB3Test.java:74)
at EJB3Test.main(EJB3Test.java:29)
WARN - SQL Error: 515, SQLState: 23000
ERROR - Cannot insert the value NULL into column 'PERSON_ID', table 'test.test.Employee'; column does not allow nulls. INSERT fails.
Name and version of the database you are using:
Microsoft SQL 2000 SP 4
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:
DEBUG - begin
DEBUG - opening JDBC connection
DEBUG - total checked-out connections: 1
DEBUG - opening new JDBC connection
DEBUG - created connection to: jdbc:jtds:sqlserver://localhost/test, Isolation Level: 2
DEBUG - current autocommit status: true
DEBUG - disabling autocommit
DEBUG - after transaction begin
DEBUG - Transaction activated, move to FlushMode AUTO
DEBUG - setting flush mode to: AUTO
DEBUG - transient instance of: domain.Employee
DEBUG - saving transient instance
DEBUG - saving [domain.Employee#<null>]
DEBUG - executing insertions
DEBUG - id unsaved-value strategy UNDEFINED
DEBUG - Getting current persistent state for: [domain.Department#Boekhouding]
DEBUG - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG - select department_.name from Department department_ where department_.name=?
Hibernate: select department_.name from Department department_ where department_.name=?
DEBUG - preparing statement
DEBUG - binding 'Boekhouding' to parameter: 1
DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG - closing statement
DEBUG - Inserting entity: domain.Employee (native id)
DEBUG - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG - insert into Person (birth, city, firstname, street, name, country) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Person (birth, city, firstname, street, name, country) values (?, ?, ?, ?, ?, ?)
DEBUG - preparing statement
DEBUG - Dehydrating entity: [domain.Employee#<null>]
DEBUG - binding '1981-01-05 11:27:05' to parameter: 1
DEBUG - binding 'Veenendaal' to parameter: 2
DEBUG - binding 'Piet' to parameter: 3
DEBUG - binding 'Dorpsstraat 62' to parameter: 4
DEBUG - binding 'Vennema' to parameter: 5
DEBUG - binding 'NL' to parameter: 6
DEBUG - Natively generated identity: 2
DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG - closing statement
DEBUG - Inserting entity: [domain.Employee#2]
DEBUG - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG - insert into Employee (department_name, id) values (?, ?)
Hibernate: insert into Employee (department_name, id) values (?, ?)
DEBUG - preparing statement
DEBUG - Dehydrating entity: [domain.Employee#2]
DEBUG - binding null to parameter: 1
DEBUG - binding '2' to parameter: 2
DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG - closing statement
DEBUG - could not insert: [domain.Employee] [insert into Employee (department_name, id) values (?, ?)]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 6:53 am 
Newbie

Joined: Tue Mar 07, 2006 7:20 am
Posts: 16
Should I open a bug report for this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 10:34 am 
Newbie

Joined: Wed Mar 08, 2006 9:36 am
Posts: 4
I may be reaching here, but don't you need to specify an unsaved-value strategy besides id generation strategy in your class? Also, I think you may need a setter for the id, even if it is private.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 11:11 am 
Newbie

Joined: Tue Mar 07, 2006 7:20 am
Posts: 16
Thank you for your thoughts

Well the set() and get() methods for the id and all the other fields are all there. (I left them out for improved reading claritiy)

I never heard of any unsaved value strategy before. I have looked it up with Google but it does not show much. Are you sure that that is a part of EJB3?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 12, 2006 4:50 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
@ManyToOne(optional=false)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 11:35 am 
Newbie

Joined: Tue Mar 07, 2006 7:20 am
Posts: 16
Hm,

I found out that something else is the problem, which seems to me like be a BUG.

Point is that after removing this line

Quote:
@Inheritance(strategy=InheritanceType.JOINED)


the problem is solved...

Emmanuel, can I get my credit back? ;-)

Nevertheless thanks for your time!


Top
 Profile  
 
 Post subject: inheritnace problem gone...
PostPosted: Thu Mar 16, 2006 11:26 am 
Newbie

Joined: Tue Mar 07, 2006 7:20 am
Posts: 16
I don't know what exactly has happened but I cannot reproduce my own problem again.

Well, problem solved.

I think it was something like old database tables/constraints lying around. But that's just guessing since I have not been able to reproduce it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.