this.log( Level.INFO, "title" );
try
{
final EntityMappings orm = new EntityMappings();
orm.setVersion( ORM_VERSION );
this.mappedClasses.clear();
for ( ClassOutline c : model.getClasses() )
{
this.toMappedSuperclass( model, c, orm );
}
for ( ClassOutline c : model.getClasses() )
{
this.toEmbeddable( c, orm );
}
for ( ClassOutline c : model.getClasses() )
{
this.toEntity( model, c, orm );
}
this.customizeOrm( model.getModel().getCustomizations(), orm );
for ( PackageOutline p : model.getAllPackageContexts() )
{
this.generateAdapterMethods( model.getCodeModel(), p );
}
for ( Entity e : orm.getEntity() )
{
if ( e.getAttributes() == null )
{
e.setAttributes( new Attributes() );
}
this.addMandatoryAttributes( orm, e.getAttributes(), this.getClassOutline( model, e.getClazz() ) );
}
final Persistence p = new Persistence();
final Persistence.PersistenceUnit u = new Persistence.PersistenceUnit();
u.setName( this.persistenceUnitName );
p.setVersion( PERSISTENCE_VERSION );
p.getPersistenceUnit().add( u );
this.customizePersistenceUnit( model.getModel().getCustomizations(), u );
for ( Entity e : orm.getEntity() )
{
if ( !u.getClazz().contains( e.getClazz() ) )
{
u.getClazz().add( e.getClazz() );
}
}
for ( Embeddable e : orm.getEmbeddable() )
{
if ( !u.getClazz().contains( e.getClazz() ) )
{
u.getClazz().add( e.getClazz() );
}
}
for ( MappedSuperclass m : orm.getMappedSuperclass() )
{
if ( !u.getClazz().contains( m.getClazz() ) )
{
u.getClazz().add( m.getClazz() );
}
}
this.annotate( model, orm );
orm.getEmbeddable().clear();
orm.getEntity().clear();
orm.getMappedSuperclass().clear();
final SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
final Schema persistenceSchema =
schemaFactory.newSchema( this.getClass().getResource( PERSISTENCE_SCHEMA_CLASSPATH_LOCATION ) );
final Schema ormSchema =
schemaFactory.newSchema( this.getClass().getResource( ORM_SCHEMA_CLASSPATH_LOCATION ) );
final JAXBContext persistenceCtx = JAXBContext.newInstance( "net.sourceforge.jpaxjc.ns.persistence" );
final JAXBContext ormCtx = JAXBContext.newInstance( "net.sourceforge.jpaxjc.ns.persistence.orm" );
final Marshaller persistenceMarshaller = persistenceCtx.createMarshaller();
final Marshaller ormMarshaller = ormCtx.createMarshaller();
persistenceMarshaller.setSchema( persistenceSchema );
persistenceMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
persistenceMarshaller.setProperty( Marshaller.JAXB_SCHEMA_LOCATION,
PERSISTENCE_NS + ' ' + this.persistenceSystemId );
ormMarshaller.setSchema( ormSchema );
ormMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
ormMarshaller.setProperty( Marshaller.JAXB_SCHEMA_LOCATION, ORM_NS + ' ' + this.ormSystemId );
final File metaInf = new File( this.persistenceUnitRoot, "META-INF" );
if ( !metaInf.exists() )
{
success = metaInf.mkdirs();
}
if ( orm.getAccess() != null
|| orm.getCatalog() != null
|| orm.getDescription() != null
|| !orm.getNamedNativeQuery().isEmpty()
|| !orm.getNamedQuery().isEmpty()
|| orm.getPackage() != null
|| orm.getPersistenceUnitMetadata() != null
|| orm.getSchema() != null
|| !orm.getSequenceGenerator().isEmpty()
|| !orm.getSqlResultSetMapping().isEmpty()
|| !orm.getTableGenerator().isEmpty()
|| !orm.getEmbeddable().isEmpty()
|| !orm.getEntity().isEmpty()
|| !orm.getMappedSuperclass().isEmpty() )
{
final File ormFile = new File( metaInf, this.persistenceUnitName + ".xml" );
this.log( Level.INFO, "writing", ormFile.getAbsolutePath() );
ormMarshaller.marshal( orm, ormFile );