-->
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.  [ 4 posts ] 
Author Message
 Post subject: Best way to test generated Hibernate Tools code
PostPosted: Tue Jan 06, 2009 12:05 pm 
Newbie

Joined: Sat Jan 05, 2008 12:29 pm
Posts: 3
Location: Bordeaux, France
Hi,

I was using Eclipse 3.3 with Hibernate Synchronizer to generate the DTOs and DAOs and everything worked just fine. The thing is, the generated classes use deprecated code, so now the a new project is starting we're changing to Eclipse 3.4 and Hibernate Tools.

I already downloaded all the plugins, so I used Hibernate Code Generation to generate (well, duh...) the Domain Code, DAO Code with "Java 5 syntax" and "Generate EJB3 Annotations" turned on. The hibernate.cfg.xml that I'm using is :

Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

      <!-- local connection properties -->
      <property name="hibernate.connection.url">***</property>
      <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
      <property name="hibernate.connection.username">sa</property>
      <property name="hibernate.connection.password">nt</property>
      <!-- property name="hibernate.connection.pool_size"></property -->

      <!-- dialect for Microsoft SQL Server -->
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
       
      <!-- Enable Hibernate's automatic session context management -->
      <property name="current_session_context_class">thread</property>
       
        <!-- Mappings -->
        <mapping class="com.sqli.cdgp.model.P22Cco"/>
    </session-factory>
   
</hibernate-configuration>


The domain code is :
Code:
package com.sqli.cdgp.model;

// Generated 6 janv. 2009 15:18:54 by Hibernate Tools 3.2.4.CR1

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
* P22Cco generated by hbm2java
*/
@Entity
@Table(name = "P22_CCO", schema = "dbo", catalog = "CDGP")
public class P22Cco implements java.io.Serializable {

   private long p22LinId;
   private String p22VchMontant;
   private String p22VchMensualite;
   private String p22VchDuree;
   private String p22VchProjet;
   private String p22VchPage;
   private Date p22DateDemande;
   private Boolean p22BolMail;
   private Long p13LinId;
   private String p22VchProvenanceOrigine;
   private String p22VchProvenanceCourante;
   private String p22VchDestinaryEmail;
   private String p22VchProjetDetaille;
   private String p22VchAutoTypeVehicule;
   private String p22VchAutoDateCirculation;
   private String p22VchSiteProvenance;
   private Long p29LinId;
   private String p22UserAgent;
   private Boolean p22FlagValidation;
   private Integer p22Etape;
   private Boolean p22BatchOk;
   private String p22VchMontantFinSouhaite;

   public P22Cco() {
   }

   public P22Cco(long p22LinId) {
      this.p22LinId = p22LinId;
   }

   public P22Cco(long p22LinId, String p22VchMontant, String p22VchMensualite,
         String p22VchDuree, String p22VchProjet, String p22VchPage,
         Date p22DateDemande, Boolean p22BolMail, Long p13LinId,
         String p22VchProvenanceOrigine, String p22VchProvenanceCourante,
         String p22VchDestinaryEmail, String p22VchProjetDetaille,
         String p22VchAutoTypeVehicule, String p22VchAutoDateCirculation,
         String p22VchSiteProvenance, Long p29LinId, String p22UserAgent,
         Boolean p22FlagValidation, Integer p22Etape, Boolean p22BatchOk,
         String p22VchMontantFinSouhaite) {
      this.p22LinId = p22LinId;
      this.p22VchMontant = p22VchMontant;
      this.p22VchMensualite = p22VchMensualite;
      this.p22VchDuree = p22VchDuree;
      this.p22VchProjet = p22VchProjet;
      this.p22VchPage = p22VchPage;
      this.p22DateDemande = p22DateDemande;
      this.p22BolMail = p22BolMail;
      this.p13LinId = p13LinId;
      this.p22VchProvenanceOrigine = p22VchProvenanceOrigine;
      this.p22VchProvenanceCourante = p22VchProvenanceCourante;
      this.p22VchDestinaryEmail = p22VchDestinaryEmail;
      this.p22VchProjetDetaille = p22VchProjetDetaille;
      this.p22VchAutoTypeVehicule = p22VchAutoTypeVehicule;
      this.p22VchAutoDateCirculation = p22VchAutoDateCirculation;
      this.p22VchSiteProvenance = p22VchSiteProvenance;
      this.p29LinId = p29LinId;
      this.p22UserAgent = p22UserAgent;
      this.p22FlagValidation = p22FlagValidation;
      this.p22Etape = p22Etape;
      this.p22BatchOk = p22BatchOk;
      this.p22VchMontantFinSouhaite = p22VchMontantFinSouhaite;
   }

   @Id
   @Column(name = "P22_LIN_ID", unique = true, nullable = false, precision = 18, scale = 0)
   public long getP22LinId() {
      return this.p22LinId;
   }

   public void setP22LinId(long p22LinId) {
      this.p22LinId = p22LinId;
   }

   @Column(name = "P22_VCH_MONTANT", length = 50)
   public String getP22VchMontant() {
      return this.p22VchMontant;
   }

   public void setP22VchMontant(String p22VchMontant) {
      this.p22VchMontant = p22VchMontant;
   }

   @Column(name = "P22_VCH_MENSUALITE", length = 50)
   public String getP22VchMensualite() {
      return this.p22VchMensualite;
   }

   public void setP22VchMensualite(String p22VchMensualite) {
      this.p22VchMensualite = p22VchMensualite;
   }

   @Column(name = "P22_VCH_DUREE", length = 50)
   public String getP22VchDuree() {
      return this.p22VchDuree;
   }

   public void setP22VchDuree(String p22VchDuree) {
      this.p22VchDuree = p22VchDuree;
   }

   @Column(name = "P22_VCH_PROJET", length = 50)
   public String getP22VchProjet() {
      return this.p22VchProjet;
   }

   public void setP22VchProjet(String p22VchProjet) {
      this.p22VchProjet = p22VchProjet;
   }

   @Column(name = "P22_VCH_PAGE", length = 2)
   public String getP22VchPage() {
      return this.p22VchPage;
   }

   public void setP22VchPage(String p22VchPage) {
      this.p22VchPage = p22VchPage;
   }

   @Temporal(TemporalType.TIMESTAMP)
   @Column(name = "P22_DATE_DEMANDE", length = 23)
   public Date getP22DateDemande() {
      return this.p22DateDemande;
   }

   public void setP22DateDemande(Date p22DateDemande) {
      this.p22DateDemande = p22DateDemande;
   }

   @Column(name = "P22_BOL_MAIL")
   public Boolean getP22BolMail() {
      return this.p22BolMail;
   }

   public void setP22BolMail(Boolean p22BolMail) {
      this.p22BolMail = p22BolMail;
   }

   @Column(name = "P13_LIN_ID", precision = 18, scale = 0)
   public Long getP13LinId() {
      return this.p13LinId;
   }

   public void setP13LinId(Long p13LinId) {
      this.p13LinId = p13LinId;
   }

   @Column(name = "P22_VCH_PROVENANCE_ORIGINE", length = 100)
   public String getP22VchProvenanceOrigine() {
      return this.p22VchProvenanceOrigine;
   }

   public void setP22VchProvenanceOrigine(String p22VchProvenanceOrigine) {
      this.p22VchProvenanceOrigine = p22VchProvenanceOrigine;
   }

   @Column(name = "P22_VCH_PROVENANCE_COURANTE", length = 100)
   public String getP22VchProvenanceCourante() {
      return this.p22VchProvenanceCourante;
   }

   public void setP22VchProvenanceCourante(String p22VchProvenanceCourante) {
      this.p22VchProvenanceCourante = p22VchProvenanceCourante;
   }

   @Column(name = "P22_VCH_DESTINARY_EMAIL", length = 100)
   public String getP22VchDestinaryEmail() {
      return this.p22VchDestinaryEmail;
   }

   public void setP22VchDestinaryEmail(String p22VchDestinaryEmail) {
      this.p22VchDestinaryEmail = p22VchDestinaryEmail;
   }

   @Column(name = "P22_VCH_PROJET_DETAILLE", length = 100)
   public String getP22VchProjetDetaille() {
      return this.p22VchProjetDetaille;
   }

   public void setP22VchProjetDetaille(String p22VchProjetDetaille) {
      this.p22VchProjetDetaille = p22VchProjetDetaille;
   }

   @Column(name = "P22_VCH_AUTO_TYPE_VEHICULE", length = 100)
   public String getP22VchAutoTypeVehicule() {
      return this.p22VchAutoTypeVehicule;
   }

   public void setP22VchAutoTypeVehicule(String p22VchAutoTypeVehicule) {
      this.p22VchAutoTypeVehicule = p22VchAutoTypeVehicule;
   }

   @Column(name = "P22_VCH_AUTO_DATE_CIRCULATION", length = 100)
   public String getP22VchAutoDateCirculation() {
      return this.p22VchAutoDateCirculation;
   }

   public void setP22VchAutoDateCirculation(String p22VchAutoDateCirculation) {
      this.p22VchAutoDateCirculation = p22VchAutoDateCirculation;
   }

   @Column(name = "P22_VCH_SITE_PROVENANCE", length = 10)
   public String getP22VchSiteProvenance() {
      return this.p22VchSiteProvenance;
   }

   public void setP22VchSiteProvenance(String p22VchSiteProvenance) {
      this.p22VchSiteProvenance = p22VchSiteProvenance;
   }

   @Column(name = "P29_LIN_ID", precision = 18, scale = 0)
   public Long getP29LinId() {
      return this.p29LinId;
   }

   public void setP29LinId(Long p29LinId) {
      this.p29LinId = p29LinId;
   }

   @Column(name = "P22_USER_AGENT", length = 200)
   public String getP22UserAgent() {
      return this.p22UserAgent;
   }

   public void setP22UserAgent(String p22UserAgent) {
      this.p22UserAgent = p22UserAgent;
   }

   @Column(name = "P22_FLAG_VALIDATION")
   public Boolean getP22FlagValidation() {
      return this.p22FlagValidation;
   }

   public void setP22FlagValidation(Boolean p22FlagValidation) {
      this.p22FlagValidation = p22FlagValidation;
   }

   @Column(name = "P22_ETAPE")
   public Integer getP22Etape() {
      return this.p22Etape;
   }

   public void setP22Etape(Integer p22Etape) {
      this.p22Etape = p22Etape;
   }

   @Column(name = "P22_BATCH_OK")
   public Boolean getP22BatchOk() {
      return this.p22BatchOk;
   }

   public void setP22BatchOk(Boolean p22BatchOk) {
      this.p22BatchOk = p22BatchOk;
   }

   @Column(name = "P22_VCH_MONTANT_FIN_SOUHAITE", length = 50)
   public String getP22VchMontantFinSouhaite() {
      return this.p22VchMontantFinSouhaite;
   }

   public void setP22VchMontantFinSouhaite(String p22VchMontantFinSouhaite) {
      this.p22VchMontantFinSouhaite = p22VchMontantFinSouhaite;
   }

}



The DAO code is :
Code:
package com.sqli.cdgp.model;

// Generated 6 janv. 2009 15:18:28 by Hibernate Tools 3.2.4.CR1

import static org.hibernate.criterion.Example.create;

import java.util.List;

import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;

/**
* Home object for domain model class P22Cco.
* @see com.sqli.cdgp.model.P22Cco
* @author Hibernate Tools
*/
public class P22CcoHome {

   private static final Log log = LogFactory.getLog(P22CcoHome.class);

   private final SessionFactory sessionFactory = getSessionFactory();

   protected SessionFactory getSessionFactory() {
      try {
         return (SessionFactory) new InitialContext()
               .lookup("SessionFactory");
      } catch (Exception e) {
         log.error("Could not locate SessionFactory in JNDI", e);
         throw new IllegalStateException(
               "Could not locate SessionFactory in JNDI");
      }
   }

   public void persist(P22Cco transientInstance) {
      log.debug("persisting P22Cco instance");
      try {
         sessionFactory.getCurrentSession().persist(transientInstance);
         log.debug("persist successful");
      } catch (RuntimeException re) {
         log.error("persist failed", re);
         throw re;
      }
   }

   public void attachDirty(P22Cco instance) {
      log.debug("attaching dirty P22Cco instance");
      try {
         sessionFactory.getCurrentSession().saveOrUpdate(instance);
         log.debug("attach successful");
      } catch (RuntimeException re) {
         log.error("attach failed", re);
         throw re;
      }
   }

   public void attachClean(P22Cco instance) {
      log.debug("attaching clean P22Cco instance");
      try {
         sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
         log.debug("attach successful");
      } catch (RuntimeException re) {
         log.error("attach failed", re);
         throw re;
      }
   }

   public void delete(P22Cco persistentInstance) {
      log.debug("deleting P22Cco instance");
      try {
         sessionFactory.getCurrentSession().delete(persistentInstance);
         log.debug("delete successful");
      } catch (RuntimeException re) {
         log.error("delete failed", re);
         throw re;
      }
   }

   public P22Cco merge(P22Cco detachedInstance) {
      log.debug("merging P22Cco instance");
      try {
         P22Cco result = (P22Cco) sessionFactory.getCurrentSession().merge(
               detachedInstance);
         log.debug("merge successful");

         return result;
      } catch (RuntimeException re) {
         log.error("merge failed", re);
         throw re;
      }
   }

   public P22Cco findById(long id) {
      log.debug("getting P22Cco instance with id: " + id);
      try {
         P22Cco instance = (P22Cco) sessionFactory.getCurrentSession().get(
               "com.sqli.cdgp.model.P22Cco", id);
         if (instance == null) {
            log.debug("get successful, no instance found");
         } else {
            log.debug("get successful, instance found");
         }
         return instance;
      } catch (RuntimeException re) {
         log.error("get failed", re);
         throw re;
      }
   }

   public List<P22Cco> findByExample(P22Cco instance) {
      log.debug("finding P22Cco instance by example");
      try {
         List<P22Cco> results = (List<P22Cco>) sessionFactory
               .getCurrentSession().createCriteria(
                     "com.sqli.cdgp.model.P22Cco").add(create(instance))
               .list();
         log.debug("find by example successful, result size: "
               + results.size());
         return results;
      } catch (RuntimeException re) {
         log.error("find by example failed", re);
         throw re;
      }
   }
}


What I'm wondering now is how do I test this code. I tried filling some attributs from the P22Cco object and using the <P22CcoHome>.persist(...), but it doesn't work. Please note that if I manually load the AnnotationConfiguration, and use the classic save methods everything works. I'm just having trouble using the generated DAOs.

I'm guessing they're generated that way for a reason but could someone please tell me how am I supposed to use that code?

NOTE: I'm not using Spring nor J22, and all my previous experience with Hibernate was with Hibernate Synchronizer.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 2:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
when you say doesnt work - what does not work ? stacktrace etc?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 7:22 pm 
Newbie

Joined: Sat Jan 05, 2008 12:29 pm
Posts: 3
Location: Bordeaux, France
The thing is, to be able to use the generated code I find myself having to create a transaction and the like, wich I fear might not be the most optimal way of using such code (the one that has just been created).

My question is, for all you more experienced Hibernate users: how would you go about using this code?, would you create a HibernateUtils class that retrieves the current session?, what the minimal/best way of using the persist (for example) method that has just been created?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2009 5:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Why is having clear transaction boundaries a bad thing ?

HibernateUtil's is not how you should get session's in Hibernate 3. See http://www.hibernate.org/42.html, docs and/or Java Persistence with Hibernate to see how to use Sessions/Transacations.

_________________
Max
Don't forget to rate


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