-->
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.  [ 29 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: Wed Aug 23, 2006 8:08 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
Ok thanks, this works fine! Now i want to save a reference from Ressource in the class Termin. Please look here:

Code:
public class Termin implements Serializable {

   private Ressource ressource; <-the reference
   private long id;
   private String kommentar; 

   public Ressource getRessource()
   {
      return ressource;
   }
   
   
   public void setRessource(Ressource r)
   {
      ressource = r;
   }
   
   
   public long getId() {
      return id;
   }

   
   public void setId(long id) {
      this.id = id;
   }
   
   
   public String getKommentar() {
      return kommentar;
   }

   
   public void setKommentar(String kommentar) {
      this.kommentar = kommentar;
   }



Termin.hbm.xml:
Code:
<hibernate-mapping>

   <class name="uebung9bPackage.Termin" table="termin">
   
    <id name="id" column="id" type="long">
        <generator class="native"/>
    </id>
   
   <property name="kommentar" column="kommentar" type="string" />
      
   <many-to-one class="uebung9bPackage.Ressource" name="ressource" column="resid" cascade="all"/>   
   
</class>

</hibernate-mapping>



Table "Termin":
Code:
CREATE TABLE `termin` (
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`kommentar` VARCHAR( 100 ) NOT NULL ,
`resid` BIGINT NOT NULL
) TYPE = innodb;



When i try to save the class Termin like this:
Code:
Termin term = new Termin();
Raum raum = new Raum();
term.setRessource(raum);
session.save(term);


this error shows up:
Code:
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [uebung9bPackage.Termin]
Caused by: java.sql.SQLException: Column 'resid' cannot be null



Hope you can help me with this last problem. Thanks!!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 5:23 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I think that you'll need to remove the "not null" constraint on the resid column of the Termin table, because term is saved before raum, so there's no id to put into the resid column at that point.

Alternatively, you could manually save raum, then save term. The cascade will work once raum has been saved once, because at that point it has an id to put into the resid column.

Finally, you could try putting the not-null="true" attribute on the many-to-one. I don't think that it does anything at run time (it's for DDL generation), but maybe it works like not-null="true" on <key> elements, determining the order of saves. If that works, it would be the best solution.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 5:48 pm 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
Hello, changing the column "resid" from table "termin" to "null" was the key to this solution! Now it works fine! Thank you very very much!!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 6:25 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
I have another question, hope you can help me again!

When i want to load the class "termin" by its ID like this:

Code:
Termin term = (Termin) session.load(Termin.class, 1);


i dont know if the reference from class "termin" is of the type "raum" or "gegenstand". How can i differentiate between "raum" and "gegenstand"?
So what did i have to do, to load the right class after i have loaded the class "termin" ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 5:42 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Frequently there'd be a "type" object (mapping to a row in a lookup table) that you could use, like this:
Code:
Ressource r = term.getRessource();
if (r.getType() == RessType.RAUM)
{
  Raum raum = (Raum) r;
}
// etc., etc.
If you don't have a type object like that, then use instanceof:
Code:
Ressource r = term.getRessource();
if (r instanceof Raum)
{
  Raum raum = (Raum) r;
}
// etc., etc.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 25, 2006 10:42 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
tenwit wrote:
Code:
Ressource r = term.getRessource();
if (r instanceof Raum)
{
  Raum raum = (Raum) r;
}
// etc., etc.


I tried this, but it did not work. There is no error message. Here my test-Code, where ID is the ID from the class "Termin".

Code:
Object qterm = session.createQuery("from Termin t where t.id =" +id).uniqueResult();

if (qterm==null)
{
   System.out.println("Termin: " + id + " not found!");
}
else
{
   
   // This works      
   Termin term = (Termin)qterm;
   Ressource r = term.getRessource();
   

   // But i think this doesnt work :-(      
   if (r instanceof Raum)
   {
       Raum raum = (Raum) r;
      System.out.println("It is a room");
   }

   if (r instanceof Gegenstand)
   {
      Gegenstand g = (Gegenstand) r;
      System.out.println("It is a thing");
   }
}


Any idea?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 26, 2006 6:16 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Perhaps the entity is still proxied? Try "Hibernate.initialize(r);", then use instanceof.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 26, 2006 10:45 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
I tried this, and it does not work.

Code:
...
else
{
            
   Termin term = (Termin)qterm;
   Ressource r = term.getRessource();
            
   Hibernate.initialize(term);  // LOOK HERE
   Hibernate.initialize(r);       // AND HERE
         
   if (r instanceof Raum)
   {
       Raum raum = (Raum) r;
       System.out.println("it is a room");
   }
   if (r instanceof Gegenstand)
   {
      Gegenstand g = (Gegenstand) r;
       System.out.println("it is a thing");
   }
}



Hibernate use this querys:
Code:
Hibernate: select termin0_.id as id3_, termin0_.kommentar as kommentar3_, termin0_.resid as resid3_ from termin termin0_ where termin0_.id=3

Hibernate: select ressource0_.id as id0_0_, ressource0_.name as name0_0_, ressource0_.gebaeude as gebaeude1_0_, ressource0_.wert as wert2_0_, ressource0_.clazz_ as clazz_0_ from ( select gebaeude, null as wert, name, id, 1 as clazz_ from raum union select null as gebaeude, wert, name, id, 2 as clazz_ from gegenstand ) ressource0_ where ressource0_.id=?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 27, 2006 5:51 am 
Beginner
Beginner

Joined: Sun Aug 13, 2006 8:48 am
Posts: 23
I have the same problem
now I found the reason here
Thank u guys


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 27, 2006 6:22 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
rainytooo wrote:
I have the same problem
now I found the reason here
Thank u guys

Can you post your solution here?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 27, 2006 5:34 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I've been looking through my mapping files for examples of union-subclass, and apparently I don't use it any more. So I can't say with any confidence what's wrong.

Can you access the various subclass-specific properties (gebaeude or wert, according to your earlier mapping file) via the object that hibernate returns? It might be that the hibernate-generated object (CGLib enhanced) is a subclass of Ressource (rather than of Raum or Gegenstand), and isn't converted to its real class until it's cast. If that's the case, you'll need some sort of type property or other discriminator that you can use in java code to determine the real class of the Ressource.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 7:59 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
How can i use this "type property" ? Any example for me?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 4:59 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It's just a discriminator for use by application code instead of hibernate. My post of Fri Aug 25, 2006 9:42 am shows how it's used. You'd create a new column, referencing lookup or "type" table, and either map that table too, or make an equivalent enum and use either StringValuedEnum or IntValuedEnum to map it.

Check out the wiki area for hints on mapping enums.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: Re: Problem using "union-subclass" (simple example)
PostPosted: Tue Jan 10, 2012 6:49 am 
Newbie

Joined: Tue Jan 10, 2012 6:32 am
Posts: 1
Caused by: org.hibernate.MappingException: Cannot use identity column key
generation with <union-subclass> mapping for: com.something.SuperClass
What's the best and most convenient way for me to generate the ID's? I do not
The Problem here is that you mix Table per Class inheritance and GenerationType.Auto. Consider an identity column in MSSql. It is Column-Based. In a Table-Per-Class Strategy you use one table per class and each one has an id.

Try:

@GeneratedValue(strategy = GenerationType.TABLE)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 29 posts ]  Go to page Previous  1, 2

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.