Forum Stats

  • 3,768,732 Users
  • 2,252,842 Discussions
  • 7,874,701 Comments

Discussions

Getting an exception when trying to persist Pattern object from java.util.regex package ( using Berk

Neelkamal-Oracle
Neelkamal-Oracle Member Posts: 15
edited Jun 15, 2016 12:48PM in Berkeley DB Java Edition

Hello,

we are evaluating berkeley DB Java to replace our file based storage and have the following questions


  1. Does berkeley db support Pattern class from java.util.regex package ?
    We  have a persistent class with Pattern object which when being loaded into the entity store is resulting in the below exception
    Exception: java.lang.IllegalArgumentException: Class could not be loaded or is not persistent: java.util.regex.Pattern
  2. Is there a way to avoid the default constructor requirement in each Persistent class?
  3. Are there any restrictions on the type of java classes supported by BK DB?

can someone help me with above questions ?

Thanks,

Neel

Best Answer

Answers

  • Greybird-Oracle
    Greybird-Oracle Member Posts: 2,690
    edited Jun 14, 2016 12:24PM Accepted Answer

    Hi Neel,

    With the DPL you can persist classes that are not annotated with @Persistent (or @Entity), but you need to use a PersistentProxy -- see the javadoc for this interface.

    --mark

  • Neelkamal-Oracle
    Neelkamal-Oracle Member Posts: 15
    edited Jun 15, 2016 11:30AM

    Thanks mark…that’s what I was looking for. I was able to implement a proxy class for java.util.regex.Pattern but when I try to do the same for java.sql.Timestamp I am running into this error

    Cannot embed a reference to a proxied object in the proxy

    Any thoughts ?

  • Greybird-Oracle
    Greybird-Oracle Member Posts: 2,690
    edited Jun 15, 2016 11:39AM

    There must be a mistake in your proxy, perhaps a Pattern in the Timestamp proxy, or vice-versa?  Please post the stack trace and the code for both proxies.

  • Neelkamal-Oracle
    Neelkamal-Oracle Member Posts: 15
    edited Jun 15, 2016 11:58AM

    Both of them are independent. Pattern works but Timestamp ( tried with both java.sql.Timestamp and com.sleepycat.je.utilint.Timestamp ) is giving me the error

    Proxy class for Pattern :

    @Persistent(proxyFor = Pattern.class)
    public class PersistentProxyForPattern implements PersistentProxy<Pattern> {
      Pattern pattern;
      @Override
      public Pattern convertProxy() {
      return pattern;
      }
      @Override
      public void initializeProxy(Pattern arg0) {
      pattern = arg0;
      }
    }
    

    Proxy class for Timestamp :


    @Persistent(proxyFor = Timestamp.class)
    public class BK_ProxyForTimestamp implements PersistentProxy<Timestamp> {
      Timestamp tstamp;
      @Override
      public Timestamp convertProxy() {
      return tstamp;
      }
      @Override
      public void initializeProxy(Timestamp arg0) {
      tstamp = arg0;
      }
    }
    

    Timestamp Proxy class registration

     EntityModel model = new AnnotationModel();
      model.registerClass(BK_ProxyForTimestamp.class);
      storeConfig.setModel(model); 
      store = new EntityStore(envmnt, "EntityStore", storeConfig);
    

    This is the exact message I am seeing in the console. No additional stack trace

    Cannot embed a reference to a proxied object in the proxy; for example, a collection may not be an element of the collection because collections are proxied
    
  • Greybird-Oracle
    Greybird-Oracle Member Posts: 2,690
    edited Jun 15, 2016 12:06PM

    You should be getting an error for both of these. The fields of the proxy (which are stored by the DPL) must be non-proxied objects. So for a Pattern proxy, you probably want to store it as a String field, and for a Timestamp proxy you could probably use a 'long' field.

    --mark

  • Neelkamal-Oracle
    Neelkamal-Oracle Member Posts: 15
    edited Jun 15, 2016 12:48PM

    Thanks Mark I am all set for now.

    But l still don't see any error when i use pattern object in the Pattern proxy class. I get a Null Pointer Exception when i try to retrieve the data though.

    I am attaching a sample code for your reference

    myEntity

    @Entity
    public class myEntity {
      @PrimaryKey
      private String Id;
      Pattern regex;
      public Pattern getRegex() {
      return regex;
      }
      public void setRegex(Pattern regex) {
      this.regex = regex;
      }
      public String getId() {
      return Id;
      }
      public void setId(String id) {
      Id = id;
      }
    }
    

    myDA

    public class myDA {
      PrimaryIndex<String, myEntity> pIdx;
      public myDA(EntityStore store) throws DatabaseException {
      pIdx = store.getPrimaryIndex(String.class, myEntity.class);
      } }
    

    ProxyForPattern

    @Persistent(proxyFor = Pattern.class)
    public class PersistentProxyForPattern implements PersistentProxy<Pattern> {
      Pattern pattern;
      @Override
      public Pattern convertProxy() {
      return pattern;
      }
      @Override
      public void initializeProxy(Pattern arg0) {
      pattern = arg0;
      }
    }
    

    myDriver

    public class myDriver {
      private static File envHome = new File("dbEnv");
      private Environment envmnt;
      private EntityStore store;
      private myDA sda;
    
      public void setup() throws DatabaseException {
      EnvironmentConfig envConfig = new EnvironmentConfig();
      StoreConfig storeConfig = new StoreConfig();
      envConfig.setAllowCreate(true);
      envConfig.setTransactional(true);
      storeConfig.setAllowCreate(true);
      storeConfig.setTransactional(true);
      envmnt = new Environment(envHome, envConfig);
      EntityModel model = new AnnotationModel();
      model.registerClass(PersistentProxyForPattern.class);
      storeConfig.setModel(model);
      store = new EntityStore(envmnt, "EntityStore", storeConfig);
      }
    
      public void shutdown() throws DatabaseException {
      store.close();
      envmnt.close();
      }
    
      private void putData() throws DatabaseException {
      setup();
      sda = new myDA(store);
      myEntity sec1 = new myEntity();
      Pattern regex = Pattern.compile("a*b");
      sec1.setId("id1");
      sec1.setRegex(regex);
      Transaction txn = envmnt.beginTransaction(null, null);
      try {
      sda.pIdx.put(txn, sec1);
      txn.commit();
      } catch (Exception e) {
      if (txn != null) {
      txn.abort();
      txn = null;
      }
      }
      shutdown();
      }
    
      private void getData() throws DatabaseException {
      setup();
      sda = new myDA(store);
      myEntity sec1 = sda.pIdx.get("id1");
      System.out.println("sec1 details" + " " + sec1.getId() + " " + sec1.getRegex());
      shutdown();
      }
    
      public static void main(String[] args) {
      myDriver ssp = new myDriver();
      try {
      System.out.println("Inserting data");
      ssp.putData();
      System.out.println("Inserting data");
      ssp.getData();
      } catch (DatabaseException dbe) {
      System.err.println("SimpleStorePut: " + dbe.toString());
      dbe.printStackTrace();
      } catch (Exception e) {
      System.out.println("Exception: " + e.toString());
      e.printStackTrace();
      }
      }
    }
    
This discussion has been closed.