-->
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.  [ 30 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: Wed Aug 30, 2006 5:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i give up ;)

You need to just call that .addA... method on the Configuration before the sessionfactory is created - its pure simple java code.

I assume you will need to customize something in spring to allow for this but that is a spring issue not hibernate.

and you can run schemaexport programmatically too if you really need programmatic access to cfg from your auxillaryobject.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 5:15 am 
Beginner
Beginner

Joined: Thu Aug 03, 2006 3:43 am
Posts: 45
:(((((((((((((((((((((((

i give up too... it turns out I was wrong ... the sessionFactory I was looking at is deprecated... now we use this class: org.springframework.orm.hibernate3.LocalSessionFactoryBean .... The problem is Spring is only used at runtime... I have an Ant task that's called from maven at compilation and that generates the DDL in a file.... That's where I need the information....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 5:19 am 
Beginner
Beginner

Joined: Thu Aug 03, 2006 3:43 am
Posts: 45
I think I'm going to create a new Configuration object(in the AO), add the mapping files to that and use it for what I need...
I'll post the whole thing on this forum... maybe in another thread (just for that) in case anybody else needs assigned ID generated as sequences and dosen't know how to do it...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 5:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so call some custom code from your maven build that creates the Configuration before it is handed to SchemaExport.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 9:49 am 
Beginner
Beginner

Joined: Thu Aug 03, 2006 3:43 am
Posts: 45
am I going crazy or what ....

Code:
    public static final String SEQUENCE = "sequence";
    public static final String PARAMETERS = "parameters";
    private static String CONFIG_FILE_LOCATION = "net/sourceforge/lebon/hibernate/hibernate.cfg.xml";
    private static final Configuration cfg = new Configuration();
    private String sequenceName;
    private String parameters;


Code:
        String sql="";
        cfg.configure(CONFIG_FILE_LOCATION);
        PersistentClass ps = cfg.getClassMapping("net.sourceforge.lebon.model.User");
        Iterator it = cfg.getClassMappings();

//       String pr = ps.getProperty(PARAMETERS).toString();

//        PropertiesHelper.getString(SEQUENCE, params, "hibernate_sequence");


        while (it.hasNext()){

            Properties params = cfg.getProperties();
            this.sequenceName = PropertiesHelper.getString(SEQUENCE, params, "hibernate_sequence");
            this.parameters = params.getProperty(PARAMETERS);
            String schemaName = params.getProperty("schema");
            String catalogName = params.getProperty("catalog");

//        if (sequenceName.indexOf(dialect.getSchemaSeparator() ) < 0) {
                sequenceName = Table.qualify(catalogName, schemaName, sequenceName);

            sql = sql + "\n seq " + sequenceName ;

//            sql = sql + "\n sequence: " + PropertiesHelper.getString(SEQUENCE, cfg.getProperties(), "hibernate_sequence");;
//            sql = sql + "\n\n sequence: " + dialect.getCreateSequenceStrings("SEQ_HE")[1];
//            sql = sql + "\n map: " + ps.getMetaAttribute("sequence");
            sql = sql + "\n it.next: " + it.next();
        }
        return sql;


isn't this sequenceName = PropertiesHelper.getString(SEQUENCE, params, "hibernate_sequence"); suposed to return the name of the "sequence property" ??? It returns null everytime although the mapping is valid ( I mean I can get the name of the entitys, but not the names of sequences...)

don't mind all the redundant code... I tried allot of stuff but I can't seem to get the sequence name that's set in the mapping files...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 9:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
huh? why do you think cfg.getProperties() should contain the sequence name ?

and weren't you using assigned as the strategy ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 9:54 am 
Beginner
Beginner

Joined: Thu Aug 03, 2006 3:43 am
Posts: 45
well yes...

Code:
        <id
            name="id"
            column="ID"
            type="java.lang.Long"
        >
            <generator class="assigned">
                <param name="sequence">sequence_name</param>
            </generator>
        </id>


but this is in the mapping xml... how do I get a string with sequence_name in the AO ??????


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 9:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
cfg.getProperties() are properties for the Configuration, not properties for you id strategy.

Instead of explaining just this one particual part i'll tell you how to figure out these things in general.

Look in HbmBinder how the values are set. Look for "generator" and see where the "param"'s are stored. That technique can be applied for any piece of the mapping files.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 11:14 am 
Beginner
Beginner

Joined: Thu Aug 03, 2006 3:43 am
Posts: 45
ok... I got this far... but sadly the only thing it returns is null :((((

Code:
    public String sqlCreateString(Dialect dialect, Mapping mapping, String string, String string1) throws HibernateException {
        String sql="";
        cfg.configure(CONFIG_FILE_LOCATION);

        Iterator iter = cfg.getClassMappings();
        while ( iter.hasNext() ) {
            PersistentClass clazz = (PersistentClass) iter.next();
            Iterator props = clazz.getPropertyClosureIterator();
            while ( props.hasNext() ) {
                Property prop = (Property) props.next();
                if ( prop.getValue().isSimpleValue() ) {
                    Properties properties = ( (SimpleValue) prop.getValue() ).getIdentifierGeneratorProperties();
                    if (properties != null)
                        sql=sql + "\n " + properties.getProperty( "sequence" );
                    else
                        sql=sql + "\nnull\n";
                }

            }

        }

        return sql;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 1:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
try and use pc.getIdentifierProperty() instead of the simple property only closure iterator ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 3:34 am 
Beginner
Beginner

Joined: Thu Aug 03, 2006 3:43 am
Posts: 45
Quote:
try and use pc.getIdentifierProperty() instead of the simple property only closure iterator ;)

??????

dosen't that return the identifier name ?? ... like "org.hibernate.mapping.Property(id)" ???

Code:
model.setIdentifierGeneratorProperties( params );

and model is simplevalue SimpleValue... isn't the "sequence" property from <param name="sequence">sequence_name</param> suposed to be in ((SimpleValue) prop.getValue()).getIdentifierGeneratorProperties()


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 3:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh no, it returns the *Property* not just the name.

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 31, 2006 8:31 am 
Beginner
Beginner

Joined: Thu Aug 03, 2006 3:43 am
Posts: 45
I feel so stupid.. you are perfectly right, thank you once again for everything... now it works so if anybody else wants to work with assigned ID's and wants to use sequences here is the sequence generator (for the DDL)

Code:
package net.sourceforge.lebon.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.Mapping;
import org.hibernate.mapping.AuxiliaryDatabaseObject;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.SimpleValue;

import java.util.Iterator;

/**
* Created by IntelliJ IDEA.
* User: sorin
* Date: Aug 29, 2006
* Time: 9:52:32 AM
* To change this template use File | Settings | File Templates.
*/
public class SequenceCreator implements AuxiliaryDatabaseObject {

    public static final String SEQUENCE = "sequence";
    private static String CONFIG_FILE_LOCATION = "net/sourceforge/lebon/hibernate/hibernate.cfg.xml";
    private static final Configuration cfg = new Configuration();

    public void addDialectScope(String string) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public boolean appliesToDialect(Dialect dialect) {
        return dialect.supportsSequences();
    }

    public String sqlCreateString(Dialect dialect, Mapping mapping, String string, String string1) throws HibernateException {
        String sql = "";
        cfg.configure(CONFIG_FILE_LOCATION);

        Iterator iter = cfg.getClassMappings();
        while (iter.hasNext()) {
            String sequence_name = null;
            String sequence_SQL[] = null;

            PersistentClass clazz = (PersistentClass) iter.next();
            if (!clazz.isJoinedSubclass()) {
                Property pr = clazz.getIdentifierProperty();
                sequence_name = ((SimpleValue) pr.getValue()).getIdentifierGeneratorProperties().getProperty(SEQUENCE);

                sequence_SQL = dialect.getCreateSequenceStrings(sequence_name);

                sql = sql + "\n";
                for (int i = 0; i < sequence_SQL.length; i++) {
                    sql = sql + sequence_SQL[i] + ((i + 1 < sequence_SQL.length) ? ";\n" : "");
                }
            }
        }
        return sql;
    }


    public String sqlDropString(Dialect dialect, String string, String string1) {
        return "";
    }
}


and to use it you just have to add this to your mapping files:

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <database-object>
       <definition class="net.sourceforge.lebon.hibernate.SequenceCreator"/>
    </database-object>

</hibernate-mapping>


and this is the code generator I use
Code:
package net.sourceforge.lebon.dao.id.impl;

import org.hibernate.StatelessSession;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.SequenceGenerator;
import org.hibernate.type.LongType;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import net.sourceforge.lebon.dao.id.IdGenerator;
import net.sourceforge.lebon.model.*;

/**
* @author Manfred Geiler (latest modification by $Author: gem $)
* @version $Revision: 581 $ $Date: 27.06.2006 14:17:42$
*/
public class IdGeneratorImpl
    implements IdGenerator
{
    private static final Log log = LogFactory.getLog(IdGeneratorImpl.class);

    private SessionFactoryImplementor _sessionFactory;
    private Map<Class, SequenceGenerator> _sequenceGeneratorMap = new HashMap<Class,SequenceGenerator>();
    private Map<Class, SequenceGenerator> _sequenceGeneratorCacheMap = new HashMap<Class,SequenceGenerator>();

    public void setSessionFactory(SessionFactoryImplementor sessionFactory)
    {
        _sessionFactory = sessionFactory;
    }

    private SequenceGenerator getSequenceGenerator(String seqName)
    {
        Properties props = new Properties();
        props.put("sequence", seqName);
        SequenceGenerator sequenceGenerator = new SequenceGenerator();
        sequenceGenerator.configure(new LongType(), props, _sessionFactory.getDialect());
        return sequenceGenerator;
    }

    public void init()
    {
        _sequenceGeneratorMap.put(DeveloperProfile.class, getSequenceGenerator("DEVP_SEQ"));
        _sequenceGeneratorMap.put(Election.class, getSequenceGenerator("EL_SEQ"));
        _sequenceGeneratorMap.put(ElectionCategory.class, getSequenceGenerator("ELC_SEQ"));
        _sequenceGeneratorMap.put(ElectionWinners.class, getSequenceGenerator("ELW_SEQ"));
        _sequenceGeneratorMap.put(ProjectProfile.class, getSequenceGenerator("PRP_SEQ"));
        _sequenceGeneratorMap.put(Transaction.class, getSequenceGenerator("TR_SEQ"));
        _sequenceGeneratorMap.put(User.class, getSequenceGenerator("USR_SEQ"));
        _sequenceGeneratorMap.put(Vote.class, getSequenceGenerator("VT_SEQ"));
        _sequenceGeneratorMap.put(VotingRight.class, getSequenceGenerator("VTR_SEQ"));

        _sequenceGeneratorCacheMap.putAll(_sequenceGeneratorMap);
    }


    public Long createId(Object entity)
    {
        StatelessSession session = null;
        try
        {
            Class<? extends Object> entityClass = entity.getClass();
            SequenceGenerator sequenceGenerator = _sequenceGeneratorCacheMap.get(entityClass);
            if (sequenceGenerator == null)
            {
                for (Map.Entry<Class,SequenceGenerator> entry : _sequenceGeneratorMap.entrySet())
                {
                     if (entry.getKey().isAssignableFrom(entityClass))
                     {
                         sequenceGenerator = entry.getValue();
                         break;
                     }
                }
                if (sequenceGenerator == null)
                {
                    throw new IllegalArgumentException("Unsupported entity class " + entityClass);
                }
                _sequenceGeneratorCacheMap.put(entityClass, sequenceGenerator);
                log.debug("Added new SequenceGenerator for class " + entityClass + " to cache");
            }

            session = _sessionFactory.openStatelessSession();
            return (Long)sequenceGenerator.generate((SessionImplementor)session, entity);
        }
        finally
        {
            if (session != null)
            {
                session.close();
            }
        }
    }


}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 9:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
How about greating a Wiki page with this work in it? Then it might be easier for people to find.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 02, 2006 5:57 am 
Beginner
Beginner

Joined: Thu Aug 03, 2006 3:43 am
Posts: 45
That's a great idea.. I'll do it as soon as I have some spare time.. The project is open source anyway :)

Thank You


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 30 posts ]  Go to page Previous  1, 2

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.