Search code examples
javaapache-cayenne

Apache Cayenne "Class is not mapped with Cayenne"


I am using Apache Cayenne to store data and check it against files to see if the file has been changed. I currently have a method that inserts data into my database but I am receiving:

Exception in thread "main" java.lang.IllegalArgumentException: Class is not mapped with Cayenne: edu.ndsu.eci.duo_demo.persist.Integrations
    at org.apache.cayenne.access.DataContext.newObject(DataContext.java:471)
    at edu.ndsu.eci.duo_demo.util.IntegrationService.insertData(IntegrationService.java:31)
    at edu.ndsu.eci.duo_demo.main.ServiceMain.main(ServiceMain.java:55)

My method:

public static void insertData(Integration i, ObjectContext context) {
    Integrations integ = context.newObject(Integrations.class);
    integ.setName(i.getName());
    integ.setEnrollPolicy(i.getEnrollPolicy());

    List<String> ipWhitelist = i.getIpWhitelist();
    String ip = "[";
    if(ipWhitelist.size() > 0)
      ip += "\"";
    for(int j = 0; j < ipWhitelist.size(); j++) {
      ip += ipWhitelist.get(j) + "\"";
      if(j < ipWhitelist.size()-1)
        ip += ",\"";
    }
    ip += "]";

    integ.setIpWhitelist(ip);
    context.commitChanges();
  }

Edit

My log output:

[main] DEBUG org.apache.cayenne.configuration.server.DataDomainProvider  - starting configuration loading: [cayenne.xml]
[main] DEBUG org.apache.cayenne.configuration.server.DataDomainProvider  - starting configuration loading: [cayenne.xml]
[main] INFO  org.apache.cayenne.configuration.XMLDataChannelDescriptorLoader  - Loading XML configuration resource from file:/home/stkarsch/git/duo-integration-verification/target/classes/cayenne.xml
[main] INFO org.apache.cayenne.configuration.XMLDataChannelDescriptorLoader  - Loading XML configuration resource from file:/home/stkarsch/git/duo-integration-verification/target/classes/cayenne.xml
[main] INFO  org.apache.cayenne.configuration.XMLDataChannelDescriptorLoader  - tag <domains> is unexpected at [36,2]. The following tags are allowed here: [domain]
[main] INFO org.apache.cayenne.configuration.XMLDataChannelDescriptorLoader  - tag <domains> is unexpected at [36,2]. The following tags are allowed here: [domain]
[main] DEBUG org.apache.cayenne.configuration.server.DataDomainProvider  - finished configuration loading in 22 ms.
[main] DEBUG org.apache.cayenne.configuration.server.DataDomainProvider  - finished configuration loading in 22 ms.
[main] DEBUG org.apache.cayenne.access.DataRowStore  - DataRowStore property cayenne.DataRowStore.snapshot.size = 10000
[main] DEBUG org.apache.cayenne.access.DataRowStore  - DataRowStore property cayenne.DataRowStore.snapshot.size = 10000

Initialization of my ServerRuntime:

ServerRuntime run = ServerRuntime.builder().addConfig("cayenne.xml").build();
ObjectContext context = run.getContext();

Solution

  • From the startup log it appears that you have an old "cayenne.xml" file running with a newer runtime:

     tag <domains> is unexpected at [36,2]. The following tags are allowed here: [domain] 
    

    IIRC <domains> is pre 3.1, while runtime looks like either 3.1 or 4.0. So I suggest the following:

    • Figure out what Cayenne version is in use in runtime.
    • Download Cayenne distro for this version.
    • Open your "cayenne.xml" file using CayenneModeler app from the distro. It should ask whether you want to upgrade.
    • Proceed with upgrade.
    • Very likely the project will be renamed from "cayenne.xml" to "cayenne-project.xml" So confirm what the new name is and use it when creating ServerRuntime