-->
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: Beispiel zu dynamic-component ?!?
PostPosted: Tue Aug 23, 2005 2:58 am 
Beginner
Beginner

Joined: Sun Apr 24, 2005 4:25 am
Posts: 28
Hi,

bin auf der Suche in Hibernate dynamisch (zur Laufzeit) Attribute einzufügen, bzw. zu einer Klasse hinzuzufügen.
Habe in der Hibernate Doku das Tag dynamic-component gefunden, aber leider ist die Beschreibung nicht sonderlich hilfreich.

Sollte es jemand schonmal erfolgreich getestet haben würde ich mich über einen Post sehr freuen.

Die neuen Attribute sollen persistent in einer Datenbank gehalten werden.

Danke


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 1:44 am 
Beginner
Beginner

Joined: Sun Apr 24, 2005 4:25 am
Posts: 28
Hi,

ich hab mal was hinbekommen, falls es irgend jemanden mal interessieren sollte.

cfg ist die Hibernate Configuration.

Code:
        try {

            // The "sql" data types.
            Type type = new StringType();

            //Type dynamicType = new DynamicComponentType(new String[]{"bar"}, new Type[]{type}, new int[]{-1}, new Cascades.CascadeStyle[]{Cascades.STYLE_NONE});

            //Type dynamicType = new DynamicComponentType(new String[]{"foo", "bar"}, new Type[]{type, type}, new int[]{-1}, new Cascades.CascadeStyle[]{Cascades.STYLE_NONE, Cascades.STYLE_NONE});


            Type dynamicType = new DynamicComponentType(new String[]{"foo", "bar", "tender"},
                    new Type[]{type, type, type}, new int[]{-1},
                    new Cascades.CascadeStyle[]{Cascades.STYLE_NONE, Cascades.STYLE_NONE, Cascades.STYLE_NONE});


            // The persistent dynamic attribute.
            PersistentClass persistentClass = cfg.createMappings().getClass(Dynamic.class);
            Property dynamicProperty = persistentClass.getProperty("dynamicAttributes");

            // Get the dynamic component and
            Component dynamicComponent = (Component) dynamicProperty.getValue();

            dynamicComponent.setType(dynamicType);

            // Add the new attribute if the component is a dynamic component.
            if (dynamicComponent.isDynamic()) {
                dynamicComponent.addProperty(addDynamicAttribute("foo", type, persistentClass));
                dynamicComponent.addProperty(addDynamicAttribute("bar", type, persistentClass));
                dynamicComponent.addProperty(addDynamicAttribute("tender", type, persistentClass));
            }

            // Database schema update.
            try {
                //sessionFactoryBean.updateDatabaseSchema();
                new SchemaUpdate(cfg).execute(true, true);
            }
            catch (HibernateException he) {
                he.printStackTrace();
            }
        }
        catch (MappingException me) {
            me.printStackTrace();
        }


die append Methode

Code:
    private Property addDynamicAttribute(String name, Type type, PersistentClass persistentClass) {

        // Create a new db column specification.
        Column column = new Column();
        column.setName(name.toUpperCase());
        column.setType(type);

        // Add column to table also.
        persistentClass.getTable().addColumn(column);

        // Create a simple value that contains the column and will be set to the property as information.
        SimpleValue simpleValue = new SimpleValue(persistentClass.getTable());
        simpleValue.addColumn(column);
        simpleValue.setType(type);

        // Create a new property.
        Property property = new Property();
        property.setName(name);
        property.setPropertyAccessorName("property");
        property.setCascade("none");
        property.setValue(simpleValue);

        return property;
    }


und hier ein Beispielmapping.

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<!--
    The dynmic test mapping.

    @author Romsl
-->

<hibernate-mapping>

    <class name="bo.Dynamic" table="Z_DYNAMIC_COMPONENT">

        <id name="id" type="long" unsaved-value="-1">
            <column name="ID" not-null="true"/>
            <generator class="native"/>
        </id>

        <property name="name" type="string">
            <column name="NAME" not-null="true"/>
        </property>

        <dynamic-component name="dynamicAttributes"/>

    </class>

</hibernate-mapping>


Viel Spaß damit,

Romsl


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.