Conflicting modifiers .... (javax.persistence.Id, javax.persistence.ManyToOne)

#1

I have two classes a Container type class and another class that is 'contained' by that class.
The contained class, CompoundKeyMapItem, represents an alternate implementation of an object held in a Map, and uses a Compound Primary Key.
The contained class therefore uses an @IdClass.
This all works fine on Eclipselink.
However when I try and run this in ObjectDb I get the following error;

Problem Creating Database
[ObjectDB 2.2.9_05] javax.persistence.PersistenceException
Conflicting modifiers for field uk.co.his.experiment8.cmdline.objdb.CompoundKeyMapItem.container (javax.persistence.Id, javax.persistence.ManyToOne) (error 322)
at com.objectdb.jpa.EMImpl.persist(EMImpl.java:376)
at uk.co.his.experiment8.cmdline.objdb.Container.mapItemsPutNew(Container.java:64)
at uk.co.his.experiment8.cmdline.objdb.Container.generateMapItems(Container.java:54)
at uk.co.his.experiment8.cmdline.CreateDatabaseTestObjOnObjectDb.main(CreateDatabaseTestObjOnObjectDb.java:18)
Caused by: com.objectdb.o.UserException: Conflicting modifiers for field uk.co.his.experiment8.cmdline.objdb.CompoundKeyMapItem.container (javax.persistence.Id, javax.persistence.ManyToOne)
at com.objectdb.o.MSG.d(MSG.java:61)
at com.objectdb.o.JPA.d(JPA.java:385)
at com.objectdb.o.SCM.r(SCM.java:258)
at com.objectdb.o.TYS.n(TYS.java:282)
at com.objectdb.o.TYM.ae(TYM.java:512)
at com.objectdb.o.TYM.ac(TYM.java:458)
at com.objectdb.o.TYM.ao(TYM.java:777)
at com.objectdb.o.TYM.at(TYM.java:862)
at com.objectdb.o.TYM.aw(TYM.java:934)
at com.objectdb.o.OBM.bv(OBM.java:361)
at com.objectdb.o.OBM.bv(OBM.java:249)
at com.objectdb.jpa.EMImpl.persist(EMImpl.java:373)
... 3 more

What am I doing wrong?

 

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.FlushModeType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import javax.persistence.Version;

import uk.co.his.experiment8.common.TestException;

@Entity
@NamedQueries({
  @NamedQuery(name = "numberOfMapItems", query = "SELECT COUNT(x) FROM CompoundKeyMapItem x") })
public class Container  {

public static Container getInstance(EntityManager em) {
  Container c = em.find(Container.class, "1");
  if (c == null) {
   c = new Container();
   em.persist(c);
  }
  return c;
}

@Id
protected String id = "1";

@SuppressWarnings("unused")
// used by JPA
@Version
private int version;
@Basic
protected java.lang.Integer lastInstanceInMap;

protected Container() { // Empty constructor for JPA
}

public void generateMapItems(Integer numberToGen, EntityManager em)
   throws TestException {
  if (lastInstanceInMap == null) {
   lastInstanceInMap = 0;
  }
  int endCnt = lastInstanceInMap + numberToGen;
  int startCnt = lastInstanceInMap;
  while (endCnt > startCnt) {
   startCnt++;
   mapItemsPutNew("" + startCnt, new CompoundKeyMapItem(startCnt), em);
   lastInstanceInMap = startCnt;
  }
}

private CompoundKeyMapItem mapItemsPutNew(java.lang.String key,
   CompoundKeyMapItem newObj, EntityManager em) throws TestException {
  newObj.setUUID();
  newObj.setSingleton(this);
  newObj.setCachedKey(key);
  em.persist(newObj);
  return newObj;
}

public CompoundKeyMapItem getMapItem(Integer cachedKey, EntityManager em) {
  return em.find(CompoundKeyMapItem.class, new CompoundKeyMapItem.CompoundKeyMapItemId(cachedKey.toString(), id));
}

public Integer getLastMapInstanceKey() {
  return lastInstanceInMap;
}

public int getNumberOfMapItems(EntityManager em) {
  Query q = em
    .createNamedQuery("numberOfMapItems");
  q.setFlushMode(FlushModeType.AUTO);
  try {
   long l = (Long) q.getSingleResult();
   if (l > Integer.MAX_VALUE)
    return -1;
   int i = (int) l;
   return i;
  } catch (javax.persistence.NoResultException ex) {
   return -1;
  }
}
}
mport java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.ManyToOne;
import javax.persistence.Version;
@Entity
@IdClass(CompoundKeyMapItem.CompoundKeyMapItemId.class)
public class CompoundKeyMapItem  {
public static class CompoundKeyMapItemId implements Serializable {
  private static final long serialVersionUID = 0L;
  private String cachedKey;
  private String container;
  public CompoundKeyMapItemId() {}
  public CompoundKeyMapItemId(String id, String parent) {
   this.cachedKey = id;
   this.container = parent;
  }
 
  @Override
  public boolean equals(Object o) {
   if(!(o instanceof CompoundKeyMapItemId)) return false;
   CompoundKeyMapItemId other = (CompoundKeyMapItemId) o;
   return container.equals(other.container) && cachedKey.equals(other.cachedKey);
  }
  @Override
  public int hashCode() {
   //Follow bloch Item 8.
   int result = 17;
   result = 37*result + cachedKey.hashCode();
   result = 37*result + container.hashCode();
   return result;
  }
  public String getCachedKey() {
   return cachedKey;
  }
  public String getSingleton() {
   return container;
  }
}

@ManyToOne(fetch=FetchType.LAZY)
@Id
private Container container;
@Basic
@Id
private String cachedKey

@Version
private int version;
@Basic
private Integer property;

@SuppressWarnings("unused") //JPA
private CompoundKeyMapItem() {
}

public CompoundKeyMapItem(int startCnt) {
  property = startCnt;
}
public void setUUID() {
  //Do nothing we do not have a separate ID
}

public Integer getJPAVersion() {
  return version;
}
public void setSingleton(Container singleton) {
  this.container = singleton;
}

public void setCachedKey(String key) {
  this.cachedKey = key;
}
public String getCachedKey() {
  return cachedKey;
}

public void setProperty(Integer newValue) {
  property = newValue;
}
public Integer getProperty() {
  return property;
}

public String getInfo() {
  return "CompoundKeyMapItem: id=" + container.id + "/" + cachedKey + ", version=" + version + ", key=" + cachedKey + ", property=" + property;
}
}

public static void main(String[] args) throws IOException {

System.out.println("Creating Database TestObj on ObjectDb");
try
{
  javax.persistence.EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("TestObjPU");
  @SuppressWarnings("unused")
  javax.persistence.EntityManager em = emf.createEntityManager();
  em.getTransaction().begin();
  Container container = Container.getInstance(em);
  container.generateMapItems(1, em);
  em.getTransaction().commit();
  System.out.println("Created Database");
}
catch(Throwable t)
{
  System.out.println("Problem Creating Database");
  t.printStackTrace(System.out);
}
}
#2

This validation check causes more trouble than it's worth.

It was removed in a new build - 2.3.0_01. Please try it.

ObjectDB Support

Reply