Short Guide to use Derby

1. Download and install Derby 10.3.2.1 http://db.apache.org/derby/
2. Configure the helloworld project. 
2.1. Add helloworld\src\main\resources\templates\CustomDDL.xpt with content
from below
2.2. Add properties to
helloworld\src\main\resources\sculptor-generator.properties as shown below
2.3 Modify helloworld\src\main\resources\templates\SpecialCases.xpt as shown
below
3 Enable DB support in JBoss for derby
3.1 Copy $DERBY_HOME\lib\derbyclient.jar to $JBOSS_HOME\server\default\lib
3.2 Add  $JBOSS_HOME\erver\default\deploy\helloworld-derbyl-ds.xml with
content from below
4. Run mvn generate-sources -Dfornax.generator.force.execution=true and
helloworld\src\generated\resources\dbschema\SimplePrototype_derby_ddl.sql
should be created
5. Create the database schema with your favorite tool
6. Deploy and run the application as described in the CRUD Gui example

This short guide helped me with setting up a small learning/demonstrating
application. In a more complex situation (payed project with more entity
relations), I expect the CustomDDL.xpt to fail. Any errors should easily be
fixed there.

Kind Regards,
René 

File Contents</>
CustomDDL.xpt
«REM»
 * Copyright 2007 The Fornax Project Team, including the original 
 * author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
«ENDREM»

«IMPORT sculptormetamodel»
«EXTENSION extensions::helper»
«EXTENSION extensions::dbhelper»
«EXTENSION extensions::properties»

«DEFINE ddl FOR Application»
    «FILE "dbschema/" + name + "_derby_ddl.sql" TO_GEN_RESOURCES»
    «REM»Drop in reverse order«ENDREM»
    «EXPAND dropIndex FOREACH getDomainObjectsInCreateOrder(false)»
    
    «EXPAND dropTable FOREACH resolveManyToManyRelations(false)»
    «EXPAND dropTable FOREACH getDomainObjectsInCreateOrder(false)»
    
    «EXPAND createTable FOREACH getDomainObjectsInCreateOrder(true)»
    «EXPAND createTable FOREACH resolveManyToManyRelations(true)»
    
    «EXPAND idPrimaryKey FOREACH
getDomainObjectsInCreateOrder(true).select(d | d.attributes.exists(a|a.name
== "id"))»
    «EXPAND manyToManyPrimaryKey FOREACH resolveManyToManyRelations(true)»
    
    «EXPAND naturalKeyConstraint FOREACH
getDomainObjectsInCreateOrder(true).select(d | d.hasNaturalKey()) »
    «EXPAND uuidConstraint FOREACH
getDomainObjectsInCreateOrder(true).select(d | d.attributes.exists(a |
a.name == "uuid")) »
    
    «EXPAND extendsForeignKeyConstraint FOREACH
getDomainObjectsInCreateOrder(true).select(d | d.extends != null)»
    
    «EXPAND foreignKeyConstraint FOREACH
getDomainObjectsInCreateOrder(true)»
    «EXPAND foreignKeyConstraint FOREACH resolveManyToManyRelations(true)»
    
    «EXPAND index FOREACH getDomainObjectsInCreateOrder(true)»

    «ENDFILE »
«ENDDEFINE »

«DEFINE dropTable FOR DomainObject»
DROP TABLE «getDatabaseName()»;
«ENDDEFINE »


«DEFINE createTable FOR DomainObject»
  «LET references.select(r | !r.many &&
(r.to.hasOwnDatabaseRepresentation())).size != 0 AS hasOneReferences -»
  «LET getBasicTypeReferences().size != 0 AS hasContainedColumns -»
  «LET references.select(r | r.to.metaType == Enum).size != 0 AS
hasEnumColumns -»
CREATE TABLE «getDatabaseName()» (
  «EXPAND column("") FOREACH attributes SEPARATOR ",
  "-»
  «IF (attributes.size > 0) && (hasOneReferences) -»,
  «ENDIF-»
  «EXPAND foreignKeyColumn FOREACH references.select(r | !r.many &&
(r.to.hasOwnDatabaseRepresentation())) SEPARATOR ",
  "-»
  «IF ((attributes.size > 0) || hasOneReferences) && (hasContainedColumns)
-»,
  «ENDIF-»
  «EXPAND containedColumns FOREACH getBasicTypeReferences()-»
  «IF ((attributes.size > 0) || hasOneReferences || hasContainedColumns) &&
(hasEnumColumns) -»,
  «ENDIF-»
  «EXPAND enumColumn FOREACH references.select(r | r.to.metaType == Enum)
SEPARATOR ",
  "-»
  «IF extends != null-»,
  «EXPAND extendsForeignKeyColumn -»
  «ENDIF-»

);
  «ENDLET -»
  «ENDLET -»
  «ENDLET -»
«ENDDEFINE»

«DEFINE column(String prefix) FOR Attribute-»
  «prefix»«getDatabaseName()»
«getDatabaseType()»«getDatabaseTypeNullability()-»
«ENDDEFINE»

«DEFINE enumColumn FOR Reference-»
  «getDatabaseName()» «getEnumDatabaseType()»«getDatabaseTypeNullability()-»
«ENDDEFINE»


«DEFINE containedColumns FOR Reference-»
  «EXPAND column(getDatabaseName() + "_") FOREACH to.attributes SEPARATOR ",
  "-»
«ENDDEFINE»

«DEFINE idPrimaryKey FOR DomainObject»
ALTER TABLE «getDatabaseName()» ADD CONSTRAINT PK_«getDatabaseName()» 
        PRIMARY KEY («attributes.select(a | a.name ==
"id").first().getDatabaseName()»)
;
«ENDDEFINE»

«DEFINE manyToManyPrimaryKey FOR DomainObject»
ALTER TABLE «getDatabaseName()» ADD CONSTRAINT PK_«getDatabaseName()» 
        PRIMARY KEY («FOREACH references AS r SEPARATOR ",
"»«r.getForeignKeyName()»«ENDFOREACH»)
;
«ENDDEFINE»

«DEFINE foreignKeyColumn FOR Reference-»
  «IF "list" == collectionType-»
  «getDatabaseName()»_INDEX INTEGER(10),
  «ENDIF-»
  «getForeignKeyName()» «getForeignKeyType() -»
«ENDDEFINE»

«DEFINE extendsForeignKeyColumn FOR DomainObject-»
  «extends.getForeignKeyName()» «extends.getForeignKeyType()»,
«ENDDEFINE»

«DEFINE foreignKeyConstraint FOR DomainObject-»
  «EXPAND foreignKeyConstraint FOREACH references.select(r | !r.many &&
(r.to.hasOwnDatabaseRepresentation()))»
«ENDDEFINE»

«DEFINE foreignKeyConstraint FOR Reference-»
ALTER TABLE «from.getDatabaseName()» ADD CONSTRAINT
FK_«truncateLongDatabaseName(from.getDatabaseName() + "_" +
getDatabaseName())» 
        FOREIGN KEY («getForeignKeyName()») REFERENCES
«to.getRootExtends().getDatabaseName()» (ID)«
        IF (opposite != null) && opposite.isDbOnDeleteCascade()» ON DELETE
CASCADE«ENDIF»
;
«ENDDEFINE»

«DEFINE extendsForeignKeyConstraint FOR DomainObject-»
ALTER TABLE «getDatabaseName()» ADD CONSTRAINT
FK_«truncateLongDatabaseName(getDatabaseName() + "_" +
extends.getDatabaseName())» 
        FOREIGN KEY («extends.getForeignKeyName()») REFERENCES
«extends.getRootExtends().getDatabaseName()» (ID)
;
«ENDDEFINE»

«DEFINE naturalKeyConstraint FOR DomainObject-»
ALTER TABLE «getDatabaseName()»
    «IF getNaturalKeyReference() != null -»
        ADD CONSTRAINT UQ_«getDatabaseName()»_KEY UNIQUE («FOREACH 
          getNaturalKeyReference().to.attributes.select(a | a.naturalKey) AS a
SEPARATOR ",
"»«getNaturalKeyReference().getDatabaseName()»_«a.getDatabaseName()»«ENDFOREACH»)
    
    «ELSE -»
        ADD CONSTRAINT UQ_«getDatabaseName()»_KEY UNIQUE («FOREACH 
          attributes.select(a | a.naturalKey) AS a SEPARATOR ",
"»«a.getDatabaseName()»«ENDFOREACH»)
        «ENDIF»
;
«ENDDEFINE»

«DEFINE uuidConstraint FOR DomainObject-»
ALTER TABLE «getDatabaseName()»
        ADD CONSTRAINT UQ_«getDatabaseName()»_UUID UNIQUE (UUID)
;
«ENDDEFINE»

«DEFINE index FOR DomainObject-»
«EXPAND index("", this) FOREACH attributes.select(a | a.index == true)-»
«EXPAND containedColumnIndex FOREACH getBasicTypeReferences()-»
«ENDDEFINE»

«DEFINE containedColumnIndex FOR Reference-»
  «EXPAND index(getDatabaseName() + "_", from) FOREACH
to.attributes.select(a | a.index == true)»
«ENDDEFINE»

«DEFINE index(String prefix, DomainObject domainObject) FOR Attribute-»
CREATE INDEX IDX_«truncateLongDatabaseName(domainObject.getDatabaseName() +
"_" + prefix + getDatabaseName())»
    ON «domainObject.getDatabaseName()» («prefix»«getDatabaseName()» ASC)
;
«ENDDEFINE»

«DEFINE dropIndex FOR DomainObject-»
«EXPAND dropIndex("", this) FOREACH attributes.select(a | a.index == true)-»
«EXPAND dropContainedColumnIndex FOREACH getBasicTypeReferences()-»
«ENDDEFINE»

«DEFINE dropContainedColumnIndex FOR Reference-»
  «EXPAND dropIndex(getDatabaseName() + "_", from) FOREACH
to.attributes.select(a | a.index == true)»
«ENDDEFINE»

«DEFINE dropIndex(String prefix, DomainObject domainObject) FOR Attribute-»
DROP INDEX IDX_«truncateLongDatabaseName(domainObject.getDatabaseName() +
"_" + prefix + getDatabaseName())»;
«ENDDEFINE»


sculptor-generator.properties
#
# Customized properties for the Sculptor generator.
#
# In this file you can define customized properties
# for the generator. By default the properties in 
# default-sculptor-generator.properties are used,
# but you can override the properties by defining 
# them in this file. Only the properties you need 
# to change need to be defined here.
#
# Look in default-sculptor-generator.properties
# in fornax-cartridges-sculptor-generator to see 
# what properties are available.
#

# Derby specific settings
db.product=custom
db.custom.maxNameLength=27
db.custom.hibernate.dialect=org.hibernate.dialect.DerbyDialect
db.custom.onDeleteCascade=true
db.custom.type.Boolean=CHAR(1)
db.custom.type.byte=CHAR
db.custom.type.Byte=CHAR
db.custom.type.short=SMALLINT
db.custom.type.Short=SMALLINT
db.custom.type.Integer=INTEGER
db.custom.type.int=INTEGER
db.custom.type.Long=BIGINT
db.custom.type.long=BIGINT
db.custom.type.Date=DATE
db.custom.type.java.util.Date=DATE
db.custom.type.DateTime=DATE
db.custom.type.Timestamp=DATE
db.custom.type.BigDecimal=DECIMAL
db.custom.type.Double=DOUBLE
db.custom.type.double=DOUBLE
db.custom.type.String=VARCHAR
db.custom.length.String=100
db.custom.type.Enum=VARCHAR
db.custom.length.Enum=40
db.custom.type.ShortString=VARCHAR
db.custom.length.ShortString=40
javaType.ShortString=String


SpecialCases.xpt
«REM»
You can change code generation templates by using
Aspect-Oriented Programming features of oAW. You
can add AROUND advice in this file. Read 
Sculptor Developer's Guide for more information.
«ENDREM»     

«IMPORT sculptormetamodel»
«EXTENSION extensions::helper»
«EXTENSION extensions::properties»
«EXTENSION extensions::dbhelper»

«AROUND *::dummyPlaceholder FOR Object»
    «targetDef.proceed()»
«ENDAROUND»

«AROUND *::id FOR DomainObject»
  <id name="«getIdAttribute().name»" «IF getIdAttribute().getHibernateType()
!= null»type="«getIdAttribute().getHibernateType()»"«ENDIF»>
      <generator class="identity" />
  </id>
«ENDAROUND»

helloworld-derbyl-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
  <local-tx-datasource>
    <jndi-name>jdbc/HelloworldDS</jndi-name>
    <connection-url>jdbc:derby://localhost:1527/helloworld</connection-url>
    <driver-class>org.apache.derby.jdbc.ClientDriver</driver-class>
    <user-name>user</user-name>
    <password>foo</password>
  </local-tx-datasource>
</datasources>
-- 
View this message in context: 
http://www.nabble.com/Starting-Support-for-Apache-Derby-tp14804464s17564p14948002.html
Sent from the Fornax-Platform mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Fornax-developer mailing list
Fornax-developer@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/fornax-developer

Reply via email to