-->
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.  [ 6 posts ] 
Author Message
 Post subject: need advice
PostPosted: Sat Feb 10, 2007 10:08 am 
Newbie

Joined: Sat Feb 10, 2007 9:37 am
Posts: 1
I am new to Hibernate and have a question to ask. I am working on a project that I need to display Item details in a list. There will be different type of Items, such as we will have Car, Wine ... etc. Car and Wine will have common attributes like name and description, however, car may have more attributes or different attributes than Wine. Also, item will have parent and Child ralationship with Category table.

So how do I create the mapping for each different Item details.
FYI, there are no DB schemas, I am pretty much using hibernate to build that schema.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 10, 2007 5:21 pm 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
Have an abstract superclass defining all the common properties, and concrete implementation classes for the individual types.

For example I've a PersistentItem defining the Id (the primary key basically) defined as follows:

Code:
@MappedSuperclass
public abstract class PersistentItem implements PersistentItemInt {
    private long id;


    /**
     * Unique identifier/primary key field
     * ID is unique within the scope of a database and table, so effectively for a class.
     * Items that haven't been persisted have ID 0.
     * @return the unique ID
     */
    @Id
    @TableGenerator(name="primary_key_generator", table="GENERATOR_VALUES", allocationSize=1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "primary_key_generator")
    public long getId() {
        return id;
    }

    /**
     * @param newVal newVal
     */
    protected void setId(long newVal){
        id = newVal;
    }
}


with implementation classes inheriting from that:
Code:
@Entity
@Table(name="ITEMCATEGORY")
public class Category extends PersistentItem implements CategoryInt {
    private String description;

    public Category() {
    }

    public Category(String description) {
        this.description = description;
    }
   

    @Basic
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}


You can add more abstract classes to create a deeper tree (but don't overdo it).

Define an abstract method in the base class to return the data that should be shows in the list (as a String, an array of Strings, or whatever that your list implementation can parse), and implement those in the actual implementation classes.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 8:51 am 
Newbie

Joined: Mon Jun 11, 2007 8:43 am
Posts: 1
jwenting, your solution is somehow better than mine, I think I'll use yours. Regards!

_________________
donate car


Top
 Profile  
 
 Post subject: It doesn't work when we have a composite key
PostPosted: Wed Aug 01, 2007 5:19 am 
Beginner
Beginner

Joined: Fri Apr 20, 2007 10:48 am
Posts: 49
Location: France
Hi jwenting,

If you have a solution to my problem I will not miss to vote for you :-).

I have the same structure of code as yours except that I have a composite key: the problem is that the @TableGenerator doesn't generate anything.

The feature I need is to keep a history of updated entries, so I opted for a shema like this:

id: for the identifier of the item
revision: for the revision number

Naturally I'll keep in the entry infos like 1.when items were changed 2.by who etc... But the problem occures even in a very simplistic shema: here are my code snippets:

Code:
@MappedSuperclass
@IdClass(ArchiveId.class)
@EntityListeners(ArchivableListener.class)
public class Archivable {
   protected long   id;
   protected long   revision;
   protected ArchiveEntry entry;
   
   public Archivable() {
      entry = new ArchiveEntry();
   }

   @Id
   public long getId() {
      return id;
   }

   @Id
   @GeneratedValue(strategy=TABLE, generator = "archivable_rev_gen")
   @TableGenerator(name = "archivable_rev_gen", table = "archivable_rev", pkColumnName = "name",
         valueColumnName = "rev", pkColumnValue = "archivable_revision", initialValue = 1, allocationSize = 1)
...
}


Here's the archive entry declaration
Code:
@Embeddable
public class ArchiveEntry implements Serializable {

   /** @author Zied Hamdi for Into © corporation on 5 juin 07 */
   private static final long serialVersionUID = 1;

   protected Date startDate, endDate;
   protected String note;
   protected ArchiveEntry originalEntry;
...
}


The problem is that no table generator is created : look at my log:

Code:
2007-08-01 10:46:32,859 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport.execute(154) - Running hbm2ddl schema export
2007-08-01 10:46:32,859 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.execute(170) - import file not found: /import.sql
2007-08-01 10:46:32,859 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport.execute(179) - exporting generated schema to database
2007-08-01 10:46:32,859 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.execute(303) - drop table Person
2007-08-01 10:46:32,875 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.drop(288) - Unsuccessful: drop table Person
2007-08-01 10:46:32,875 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.drop(289) - 'DROP TABLE' ne peut pas être exécuté sur 'PERSON' parce qu'il n'existe pas.
2007-08-01 10:46:32,875 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.execute(303) - create table Person (revision bigint not null, id bigint not null, startDate timestamp, endDate timestamp, note varchar(255), lastOccurence smallint not null, primary key (revision, id))
2007-08-01 10:46:33,015 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport.execute(196) - schema export complete




here are the other needed files:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
   xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
   <persistence-unit name="IntoJPA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <class>fr.into.tests.Archivable</class>
      <class>fr.into.tests.ArchiveEntry</class>
      <class>fr.into.tests.Person</class>
      <!-- <jta-data-source>jdbc/intoDB_DS</jta-data-source> -->
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <properties>
         <property name="hibernate.dialect"
            value="org.hibernate.dialect.DerbyDialect" />
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.hbm2ddl.auto" value="create" />

         <property name="hibernate.connection.driver_class"
            value="org.apache.derby.jdbc.ClientDriver" />
         <property name="hibernate.connection.username" value="app" />
         <property name="hibernate.connection.password" value="app" />
         <property name="hibernate.connection.url"
            value="jdbc:derby://localhost:1527/sample" />

         <property name="hibernate.show_sql"
            value="true" />
         <!-- <property name="hibernate.use_sql_comments"
            value="true" /> -->            
      </properties>
   </persistence-unit>
</persistence>



Code:
package main;

import java.util.Date;

import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import fr.into.tests.Person;

public class JPAMain {

   public static void main(String[] args) throws NamingException {
      // InitialContext context = new InitialContext();
      // context.lookup("jdbc/intoDB_DS");
      EntityManagerFactory emf = Persistence
            .createEntityManagerFactory("IntoJPA");
      EntityManager em = emf.createEntityManager();

      try {
         EntityTransaction transaction = em.getTransaction();
         transaction.begin();
         Person zied = new Person();
         zied.setId( 1 );
         zied.getEntry().setNote( "première" );
         zied.getEntry().setStartDate( new Date() );
         em.persist(zied);
//         zied = new Person();
//         zied.setId( 1 );
//         zied.setRevision( 0 );
//         zied.getEntry().setNote( "deuxième" );
//         zied.getEntry().setStartDate( new Date() );
//         em.persist(zied);         
         transaction.commit();
      } catch (Exception ex) {
         ex.printStackTrace();
      } finally {
         em.close();
         emf.close();
      }
   }
}


any idea?

_________________
Regards,
Zied Hamdi


Top
 Profile  
 
 Post subject: It doesn't work when we have a composite key
PostPosted: Wed Aug 01, 2007 5:21 am 
Beginner
Beginner

Joined: Fri Apr 20, 2007 10:48 am
Posts: 49
Location: France
Hi jwenting,

If you have a solution to my problem I will not miss to vote for you :-).

I have the same structure of code as yours except that I have a composite key: the problem is that the @TableGenerator doesn't generate anything.

The feature I need is to keep a history of updated entries, so I opted for a shema like this:

id: for the identifier of the item
revision: for the revision number

Naturally I'll keep in the entry infos like 1.when items were changed 2.by who etc... But the problem occures even in a very simplistic shema: here are my code snippets:

Code:
@MappedSuperclass
@IdClass(ArchiveId.class)
@EntityListeners(ArchivableListener.class)
public class Archivable {
   protected long   id;
   protected long   revision;
   protected ArchiveEntry entry;
   
   public Archivable() {
      entry = new ArchiveEntry();
   }

   @Id
   public long getId() {
      return id;
   }

   @Id
   @GeneratedValue(strategy=TABLE, generator = "archivable_rev_gen")
   @TableGenerator(name = "archivable_rev_gen", table = "archivable_rev", pkColumnName = "name",
         valueColumnName = "rev", pkColumnValue = "archivable_revision", initialValue = 1, allocationSize = 1)
...
}


Here's the archive entry declaration
Code:
@Embeddable
public class ArchiveEntry implements Serializable {

   /** @author Zied Hamdi for Into © corporation on 5 juin 07 */
   private static final long serialVersionUID = 1;

   protected Date startDate, endDate;
   protected String note;
   protected ArchiveEntry originalEntry;
...
}


The problem is that no table generator is created : look at my log:

Code:
2007-08-01 10:46:32,859 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport.execute(154) - Running hbm2ddl schema export
2007-08-01 10:46:32,859 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.execute(170) - import file not found: /import.sql
2007-08-01 10:46:32,859 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport.execute(179) - exporting generated schema to database
2007-08-01 10:46:32,859 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.execute(303) - drop table Person
2007-08-01 10:46:32,875 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.drop(288) - Unsuccessful: drop table Person
2007-08-01 10:46:32,875 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.drop(289) - 'DROP TABLE' ne peut pas être exécuté sur 'PERSON' parce qu'il n'existe pas.
2007-08-01 10:46:32,875 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.execute(303) - create table Person (revision bigint not null, id bigint not null, startDate timestamp, endDate timestamp, note varchar(255), lastOccurence smallint not null, primary key (revision, id))
2007-08-01 10:46:33,015 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport.execute(196) - schema export complete




here are the other needed files:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
   xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
   <persistence-unit name="IntoJPA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <class>fr.into.tests.Archivable</class>
      <class>fr.into.tests.ArchiveEntry</class>
      <class>fr.into.tests.Person</class>
      <!-- <jta-data-source>jdbc/intoDB_DS</jta-data-source> -->
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <properties>
         <property name="hibernate.dialect"
            value="org.hibernate.dialect.DerbyDialect" />
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.hbm2ddl.auto" value="create" />

         <property name="hibernate.connection.driver_class"
            value="org.apache.derby.jdbc.ClientDriver" />
         <property name="hibernate.connection.username" value="app" />
         <property name="hibernate.connection.password" value="app" />
         <property name="hibernate.connection.url"
            value="jdbc:derby://localhost:1527/sample" />

         <property name="hibernate.show_sql"
            value="true" />
         <!-- <property name="hibernate.use_sql_comments"
            value="true" /> -->            
      </properties>
   </persistence-unit>
</persistence>



Code:
package main;

import java.util.Date;

import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import fr.into.tests.Person;

public class JPAMain {

   public static void main(String[] args) throws NamingException {
      // InitialContext context = new InitialContext();
      // context.lookup("jdbc/intoDB_DS");
      EntityManagerFactory emf = Persistence
            .createEntityManagerFactory("IntoJPA");
      EntityManager em = emf.createEntityManager();

      try {
         EntityTransaction transaction = em.getTransaction();
         transaction.begin();
         Person zied = new Person();
         zied.setId( 1 );
         zied.getEntry().setNote( "première" );
         zied.getEntry().setStartDate( new Date() );
         em.persist(zied);
//         zied = new Person();
//         zied.setId( 1 );
//         zied.setRevision( 0 );
//         zied.getEntry().setNote( "deuxième" );
//         zied.getEntry().setStartDate( new Date() );
//         em.persist(zied);         
         transaction.commit();
      } catch (Exception ex) {
         ex.printStackTrace();
      } finally {
         em.close();
         emf.close();
      }
   }
}


any idea?

_________________
Regards,
Zied Hamdi


Top
 Profile  
 
 Post subject: It doesn't work when we have a composite key
PostPosted: Wed Aug 01, 2007 5:21 am 
Beginner
Beginner

Joined: Fri Apr 20, 2007 10:48 am
Posts: 49
Location: France
Hi jwenting,

If you have a solution to my problem I will not miss to vote for you :-).

I have the same structure of code as yours except that I have a composite key: the problem is that the @TableGenerator doesn't generate anything.

The feature I need is to keep a history of updated entries, so I opted for a shema like this:

id: for the identifier of the item
revision: for the revision number

Naturally I'll keep in the entry infos like 1.when items were changed 2.by who etc... But the problem occures even in a very simplistic shema: here are my code snippets:

Code:
@MappedSuperclass
@IdClass(ArchiveId.class)
@EntityListeners(ArchivableListener.class)
public class Archivable {
   protected long   id;
   protected long   revision;
   protected ArchiveEntry entry;
   
   public Archivable() {
      entry = new ArchiveEntry();
   }

   @Id
   public long getId() {
      return id;
   }

   @Id
   @GeneratedValue(strategy=TABLE, generator = "archivable_rev_gen")
   @TableGenerator(name = "archivable_rev_gen", table = "archivable_rev", pkColumnName = "name",
         valueColumnName = "rev", pkColumnValue = "archivable_revision", initialValue = 1, allocationSize = 1)
...
}


Here's the archive entry declaration
Code:
@Embeddable
public class ArchiveEntry implements Serializable {

   /** @author Zied Hamdi for Into © corporation on 5 juin 07 */
   private static final long serialVersionUID = 1;

   protected Date startDate, endDate;
   protected String note;
   protected ArchiveEntry originalEntry;
...
}


The problem is that no table generator is created : look at my log:

Code:
2007-08-01 10:46:32,859 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport.execute(154) - Running hbm2ddl schema export
2007-08-01 10:46:32,859 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.execute(170) - import file not found: /import.sql
2007-08-01 10:46:32,859 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport.execute(179) - exporting generated schema to database
2007-08-01 10:46:32,859 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.execute(303) - drop table Person
2007-08-01 10:46:32,875 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.drop(288) - Unsuccessful: drop table Person
2007-08-01 10:46:32,875 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.drop(289) - 'DROP TABLE' ne peut pas être exécuté sur 'PERSON' parce qu'il n'existe pas.
2007-08-01 10:46:32,875 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport.execute(303) - create table Person (revision bigint not null, id bigint not null, startDate timestamp, endDate timestamp, note varchar(255), lastOccurence smallint not null, primary key (revision, id))
2007-08-01 10:46:33,015 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaExport.execute(196) - schema export complete




here are the other needed files:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
   xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
   <persistence-unit name="IntoJPA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <class>fr.into.tests.Archivable</class>
      <class>fr.into.tests.ArchiveEntry</class>
      <class>fr.into.tests.Person</class>
      <!-- <jta-data-source>jdbc/intoDB_DS</jta-data-source> -->
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <properties>
         <property name="hibernate.dialect"
            value="org.hibernate.dialect.DerbyDialect" />
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.hbm2ddl.auto" value="create" />

         <property name="hibernate.connection.driver_class"
            value="org.apache.derby.jdbc.ClientDriver" />
         <property name="hibernate.connection.username" value="app" />
         <property name="hibernate.connection.password" value="app" />
         <property name="hibernate.connection.url"
            value="jdbc:derby://localhost:1527/sample" />

         <property name="hibernate.show_sql"
            value="true" />
         <!-- <property name="hibernate.use_sql_comments"
            value="true" /> -->            
      </properties>
   </persistence-unit>
</persistence>



Code:
package main;

import java.util.Date;

import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import fr.into.tests.Person;

public class JPAMain {

   public static void main(String[] args) throws NamingException {
      // InitialContext context = new InitialContext();
      // context.lookup("jdbc/intoDB_DS");
      EntityManagerFactory emf = Persistence
            .createEntityManagerFactory("IntoJPA");
      EntityManager em = emf.createEntityManager();

      try {
         EntityTransaction transaction = em.getTransaction();
         transaction.begin();
         Person zied = new Person();
         zied.setId( 1 );
         zied.getEntry().setNote( "première" );
         zied.getEntry().setStartDate( new Date() );
         em.persist(zied);
//         zied = new Person();
//         zied.setId( 1 );
//         zied.setRevision( 0 );
//         zied.getEntry().setNote( "deuxième" );
//         zied.getEntry().setStartDate( new Date() );
//         em.persist(zied);         
         transaction.commit();
      } catch (Exception ex) {
         ex.printStackTrace();
      } finally {
         em.close();
         emf.close();
      }
   }
}


any idea?

_________________
Regards,
Zied Hamdi


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