-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate 4.1: SchemaExport with JPA2 entities
PostPosted: Tue May 22, 2012 2:40 am 
Newbie

Joined: Tue May 20, 2008 12:21 pm
Posts: 4
Hello

I'm trying to programatically generate a schema using the SchemaExport. It is working well beside the JSR-303 annotations are being ignored. This leads to a DDL containing e.g. string fields with the default length of 255.

Any idea what I'm doing wrong?

Thanks for your help!

Regards,
wibbo

Main method:
Code:
public static void main(final String[] args) {
      String databaseDialect = args[0];
      String ormFilePath = args[1];
      String outputFile = args[2];
      boolean export = Boolean.parseBoolean(args[3]);
      boolean drop = Boolean.parseBoolean(args[4]);
      boolean create = Boolean.parseBoolean(args[5]);

      Configuration configuration = new Configuration();
      configuration.setNamingStrategy(new ImprovedNamingStrategy());

      // Select the dialect matching the selected DBMSs
      configuration.setProperty("hibernate.dialect", databaseDialect);
      
      // Add each entity
      List<Class<?>> entityClasses = buildEntityClasses(ormFilePath);
      for (Class<?> entityClass : entityClasses) {
         configuration.addAnnotatedClass(entityClass);
      }

      // Dump formated SQL to console and file
      SchemaExport schemaExport = new SchemaExport(configuration);
      schemaExport.setOutputFile(outputFile);
      schemaExport.setFormat(true);
      schemaExport.setDelimiter(";");
      schemaExport.execute(true, export, drop, create);
}

private static List<Class<?>> buildEntityClasses(String ormFilePath) {
      List<Class<?>> entityClasses = new ArrayList<Class<?>>();

      try {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         File ormFile = new File(ormFilePath);

         Document doc = db.parse(ormFile);
         NodeList entities = doc.getElementsByTagName("entity");

         // iterate the employees
         for (int i = 0; i < entities.getLength(); i++) {
            Element element = (Element) entities.item(i);
            Attr classAttr = element.getAttributeNode("class");
            Class<?> entityClass = OrmBasedDdlGeneration.class.getClassLoader().loadClass(classAttr.getValue());
            entityClasses.add(entityClass);
         }
      } catch (ParserConfigurationException pce) {
         pce.printStackTrace();
      } catch (SAXException se) {
         se.printStackTrace();
      } catch (IOException ioe) {
         ioe.printStackTrace();
      } catch (ClassNotFoundException cnfe) {
         cnfe.printStackTrace();
      }

      return entityClasses;
}

Example entity class:
Code:
@Entity
@SequenceGenerator(name = "keyid_generator", sequenceName = "address_sequence")
public class Address extends AbstractIntKeyIntOptimisticLockingDto {
   /**
    * Logger instance.
    */
   private static Logger s_logger = Logger.getLogger(Address.class.getName());
   
   /**
    * The address type.
    */
   private String type;
   
   /**
    * The address line 1.
    */
   private String addressLine1;
   
   /**
    * The address line 2.
    */
   private String addressLine2;
   
   /**
    * The person.
    */
   private Person person;
   
   /**
    * Default constructor.
    */
   private Address() {
      super();
   }

   /**
    * Constructor to create a address.
    *
    * @param person of the person
    */
   public Address(Person person) {
      this();
      setPerson(person);
      
      if (null != person) {
         person.getAddresses().add(this);
      }
   }
   
   /**
    * @return Returns the type.
    */
   @NotNull
   @Size(min = 5, max = 20, message = "{address.type.size}")
   public String getType() {
      return type;
   }

   /**
    * @param type Is the type to set.
    */
   public void setType(String type) {
      this.type = type;
   }

   /**
    * @return Returns the addressLine1.
    */
   @NotNull
   @Size(min = 5, max = 50, message = "{address.addressLine1.size}")
   public String getAddressLine1() {
      return addressLine1;
   }

   /**
    * @param addressLine1 Is the addressLine1 to set.
    */
   public void setAddressLine1(String addressLine1) {
      this.addressLine1 = addressLine1;
   }

   /**
    * @return Returns the addressLine2.
    */
   @NotNull
   @Size(min = 5, max = 50, message = "{address.addressLine2.size}")
   public String getAddressLine2() {
      return addressLine2;
   }

   /**
    * @param addressLine2 Is the addressLine2 to set.
    */
   public void setAddressLine2(String addressLine2) {
      this.addressLine2 = addressLine2;
   }
   
   /**
    * @return the person
    */
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "person_key", nullable = false, unique = false, insertable = false, updatable = false)
   @ForeignKey(name = "FK_ADDRESS_PERSON")
   public Person getPerson() {
      return person;
   }

   /**
    * @param person used by the address
    */
   private void setPerson(Person person) {
      this.person = person;
   }
}

The output for this class:
Code:
create table address (
        keyid integer generated by default as identity,
        optimisticlockingversion integer,
        address_line1 varchar(255),
        address_line2 varchar(255),
        type varchar(255),
        person_key integer not null,
        primary key (keyid)
);


Top
 Profile  
 
 Post subject: Re: Hibernate 4.1: SchemaExport with JPA2 entities
PostPosted: Tue May 22, 2012 3:24 am 
Newbie

Joined: Tue May 20, 2008 12:21 pm
Posts: 4
I think my topic relates to viewtopic.php?f=1&t=1014535


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