Usually a persistence object is created via:

import objects.Product;
// persist new created product
    Product product;
    product.name = "Product name";
    product.nr = 12;
    ivy.persistence.webshpo.persist(product);

What we are trying to do (for a dynamic interface) to dynamically create the persistence object with something like:

Object persistenceObject = Class.forName(in.persistenceClassName).newInstance();

Is that generally possible? Can that be achieved in a ScriptStep or just in a JavaClass?

Edit: We tried to give the full package name in the code mentioned above which returns a ClassNotFoundException:

Object persistenceObject = Class.forName("objects.Product").newInstance();

Edit 2: Changed persistenceUnit naming to persistenceObject according to Daniel's comment.

asked 25.04.2014 at 10:39

Nikel%20Weis's gravatar image

Nikel Weis
(suspended)
accept rate: 57%

edited 25.04.2014 at 13:32

Are you sure you want to switch between persistence units?

Like I have PersistenceUnit (databaseA) and PersistenceUnit(databaseB). Now you want dynamically choose which database to use?

(25.04.2014 at 12:13) Daniel Oechslin Daniel%20Oechslin's gravatar image

I think I've made a mistake. Im not talking about a persistence unit (on database level) but on data-class level.

(25.04.2014 at 13:30) Nikel Weis Nikel%20Weis's gravatar image

Short answer: Yes.

Long answer: You need to create a Java class which returns the persistence object as an object (just ignore the IvyScript-Error):

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;

}

And then call the class from an ivyScript step:

Object persistenceUnit = helper.returnPersistenceObject("modelName");

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.

link

answered 05.05.2014 at 08:41

Nikel%20Weis's gravatar image

Nikel Weis
(suspended)
accept rate: 57%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×32

Asked: 25.04.2014 at 10:39

Seen: 4,391 times

Last updated: 05.05.2014 at 08:41