<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Is it possible to create an ivy persistence object from String?</title><link>https://answers.axonivy.com/questions/658/is-it-possible-to-create-an-ivy-persistence-object-from-string</link><description>&lt;p&gt;Usually a persistence object is created via:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import objects.Product;
// persist new created product
    Product product;
    product.name = "Product name";
    product.nr = 12;
    ivy.persistence.webshpo.persist(product);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What we are trying to do (for a dynamic interface) to dynamically create the persistence object with something like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Object persistenceObject = Class.forName(in.persistenceClassName).newInstance();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is that generally possible? Can that be achieved in a ScriptStep or just in a JavaClass?&lt;/p&gt;
&lt;p&gt;Edit:
We tried to give the full package name in the code mentioned above which returns a ClassNotFoundException:&lt;/p&gt;
&lt;p&gt;Object persistenceObject = Class.forName("objects.Product").newInstance();&lt;/p&gt;
&lt;p&gt;Edit 2:
Changed persistenceUnit naming to persistenceObject according to Daniel's comment.&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/658/is-it-possible-to-create-an-ivy-persistence-object-from-string" rel="self"></atom:link><language>en</language><lastBuildDate>Mon, 05 May 2014 08:41:20 -0400</lastBuildDate><item><title>Answer by Nikel Weis</title><link>https://answers.axonivy.com/questions/658/is-it-possible-to-create-an-ivy-persistence-object-from-string/676</link><description>&lt;p&gt;Short answer: Yes.&lt;/p&gt;
&lt;p&gt;Long answer: You need to create a Java class which returns the persistence object as an object (just ignore the IvyScript-Error):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public Object returnPersistenceObject(String persistenceClassName) {
    Object persistenceUnit = "error";
    try {
        persistenceUnit = Class.forName(persistenceClassName).newInstance();
        return persistenceUnit;
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        return "ClassNotFoundException";
        //e.printStackTrace();
    }

    return persistenceUnit;

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then call the class from an ivyScript step: &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Object persistenceUnit = helper.returnPersistenceObject("modelName");
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are some more pitfalls here but I think this is a very special use case. If someone needs more explanation just comment and I will explain more.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nikel Weis</dc:creator><pubDate>Mon, 05 May 2014 08:41:20 -0400</pubDate><guid>https://answers.axonivy.com/questions/658/is-it-possible-to-create-an-ivy-persistence-object-from-string/676</guid></item><item><title>Comment by Nikel Weis on Nikel Weis's question</title><link>https://answers.axonivy.com/questions/658/is-it-possible-to-create-an-ivy-persistence-object-from-string#664</link><description>&lt;p&gt;I think I've made a mistake. Im not talking about a persistence unit (on database level) but on data-class level.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nikel Weis</dc:creator><pubDate>Fri, 25 Apr 2014 13:30:54 -0400</pubDate><guid>https://answers.axonivy.com/questions/658/is-it-possible-to-create-an-ivy-persistence-object-from-string#664</guid></item><item><title>Comment by Daniel Oechslin on Nikel Weis's question</title><link>https://answers.axonivy.com/questions/658/is-it-possible-to-create-an-ivy-persistence-object-from-string#663</link><description>&lt;p&gt;Are you sure you want to switch between persistence units? &lt;/p&gt;
&lt;p&gt;Like I have PersistenceUnit (databaseA) and PersistenceUnit(databaseB).
Now you want dynamically choose which database to use?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Daniel Oechslin</dc:creator><pubDate>Fri, 25 Apr 2014 12:13:13 -0400</pubDate><guid>https://answers.axonivy.com/questions/658/is-it-possible-to-create-an-ivy-persistence-object-from-string#663</guid></item></channel></rss>