Announcement Announcement Module
Collapse
No announcement yet.
why isn't it persisting the references? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • why isn't it persisting the references?

    hi,

    I built a very simple data model with spring-roo. I can persist the Mce Entities and retrieve them back but the set of Tag is always null. Here is my script:

    //persistence setup --provider DATANUCLEUS --database HYPERSONIC_IN_MEMORY
    //--testAutomatically
    entity --class ~.server.domain.Tag
    field string --fieldName tag --notNull

    entity --class ~.server.domain.Mce
    field string --fieldName uri --notNull
    field set --fieldName tag --type ~.server.domain.Tag --sizeMin 1 --notNull
    field date --fieldName created --type java.util.Date
    Spring Roo does something like this:

    entityManager().createQuery("select o from Mce o")
    I get the list I wanted with non null entities but the tag field is always empty!

    I'm using 'persistence setup --provider DATA_NUCLEUS --database HYPERSONIC_IN_MEMORY'

    Any advise?

    Thank you

  • #2
    That should not happen. Any reason you are using Data Nucleus as ORM? We support it mostly for Google App Engine and VMforce. Can you change to EclipseLink or Hibernate instead to see if that makes a difference?

    Comment


    • #3
      Originally posted by Stefan Schmidt View Post
      Any reason you are using Data Nucleus as ORM? We support it mostly for Google App Engine and VMforce.
      While trying a different implementation will always be useful in debugging, it is hardly fair to imply that DataNucleus should only be used for GAE/J and VMforce (which is what was implied above). It is a fully compliant implementation of more persistence specs than any of the others and, like all persistence solutions, has its advantages. I already advised the user (on another site) to look at his log (and also quote the persistence code being used).

      Comment


      • #4
        Well this is my persistence code

        Well this is my persistence code

        Code:
            Date timeStamp = new Date();
            Mce aNewMce = new Mce();
        
            // Copy the tags from the user to the server
            for (String tag : action.getMceToServer().tag) {
                Tag newTag = new Tag();
                newTag.setTag(tag);
                newTag.setMce(aNewMce);
                aNewMce.getTag().add(newTag);
            }
        
            try {
                aNewMce.setUri(action.getMceToServer().uri);
                aNewMce.setCreated(timeStamp);
                aNewMce.persist();
        
                logger.info("I have uri  : " + aNewMce.getUri());
                logger.info("I have tags : " + aNewMce.getTag());
        
                List<Mce> findAllMce = Mce.findAllMces();
                logger.info("I have uri  : " + findAllMce.get(0).getUri());
                logger.info("I don't have tags : " + findAllMce.get(0).getTag());
            }
            catch (Exception e) {
                System.out.print(e.getMessage());
            }

        Comment


        • #5
          Debug trace #1 (seems to be table creation)

          Code:
          The server is running at http://localhost:8888/
          
          ....
          
          13:46:07,156 DEBUG [DataNucleus.Datastore.Schema] - Check of existence of MCE returned table type of null
          2011-04-01 13:46:07,156 DEBUG [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - Check of existence of MCE returned table type of null - 131156 
          13:46:07,156 INFO  [DataNucleus.Datastore.Schema] - Creating table MCE
          2011-04-01 13:46:07,156 INFO [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - Creating table MCE - 131156 
          13:46:07,156 DEBUG [DataNucleus.Datastore.Schema] - CREATE TABLE MCE
          (
              ID BIGINT NOT NULL,
              CREATED TIMESTAMP NOT NULL,
              URI VARCHAR(255) NULL,
              VERSION INTEGER NULL,
              PRIMARY KEY (ID)
          )
          2011-04-01 13:46:07,156 DEBUG [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - CREATE TABLE MCE
          (
              ID BIGINT NOT NULL,
              CREATED TIMESTAMP NOT NULL,
              URI VARCHAR(255) NULL,
              VERSION INTEGER NULL,
              PRIMARY KEY (ID)
          ) - 131156 
          13:46:07,156 DEBUG [DataNucleus.Datastore.Schema] - Execution Time = 0 ms
          2011-04-01 13:46:07,156 DEBUG [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - Execution Time = 0 ms - 131156 
          13:46:07,171 DEBUG [DataNucleus.Datastore.Schema] - Check of existence of TAG returned table type of null
          2011-04-01 13:46:07,171 DEBUG [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - Check of existence of TAG returned table type of null - 131171 
          13:46:07,171 INFO  [DataNucleus.Datastore.Schema] - Creating table TAG
          2011-04-01 13:46:07,171 INFO [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - Creating table TAG - 131171 
          13:46:07,171 DEBUG [DataNucleus.Datastore.Schema] - CREATE TABLE TAG
          (
              ID BIGINT NOT NULL,
              MCE_ID BIGINT NULL,
              TAG VARCHAR(255) NULL,
              VERSION INTEGER NULL,
              PRIMARY KEY (ID)
          )
          2011-04-01 13:46:07,171 DEBUG [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - CREATE TABLE TAG
          (
              ID BIGINT NOT NULL,
              MCE_ID BIGINT NULL,
              TAG VARCHAR(255) NULL,
              VERSION INTEGER NULL,
              PRIMARY KEY (ID)
          ) - 131171 
          13:46:07,171 DEBUG [DataNucleus.Datastore.Schema] - Execution Time = 0 ms
          2011-04-01 13:46:07,171 DEBUG [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - Execution Time = 0 ms - 131171 
          13:46:07,250 DEBUG [DataNucleus.Datastore.Schema] - Column "SEQUENCE_TABLE.SEQUENCE_NAME" added to internal representation of table.
          2011-04-01 13:46:07,250 DEBUG [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - Column "SEQUENCE_TABLE.SEQUENCE_NAME" added to internal representation of table. - 131250 
          13:46:07,265 DEBUG [DataNucleus.Datastore.Schema] - Column "SEQUENCE_TABLE.NEXT_VAL" added to internal representation of table.
          2011-04-01 13:46:07,265 DEBUG [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - Column "SEQUENCE_TABLE.NEXT_VAL" added to internal representation of table. - 131265 
          13:46:07,281 DEBUG [DataNucleus.Datastore.Schema] - Check of existence of SEQUENCE_TABLE returned table type of null
          2011-04-01 13:46:07,281 DEBUG [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - Check of existence of SEQUENCE_TABLE returned table type of null - 131281 
          13:46:07,281 INFO  [DataNucleus.Datastore.Schema] - Creating table SEQUENCE_TABLE
          2011-04-01 13:46:07,281 INFO [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - Creating table SEQUENCE_TABLE - 131281 
          13:46:07,281 DEBUG [DataNucleus.Datastore.Schema] - CREATE TABLE SEQUENCE_TABLE
          (
              SEQUENCE_NAME VARCHAR(255) NOT NULL,
              NEXT_VAL BIGINT NOT NULL,
              PRIMARY KEY (SEQUENCE_NAME)
          )
          2011-04-01 13:46:07,281 DEBUG [28714143@qtp-9652274-0] DataNucleus.Datastore.Schema - CREATE TABLE SEQUENCE_TABLE
          (
              SEQUENCE_NAME VARCHAR(255) NOT NULL,
              NEXT_VAL BIGINT NOT NULL,
              PRIMARY KEY (SEQUENCE_NAME)
          ) - 131281 
          13:46:07,281 DEBUG [DataNucleus.Datastore.Schema] - Execution Time = 0 ms

          Comment


          • #6
            Too long of a log... (see attachment)

            I attached my DEBUG level trace, was too long for any other site. But I probable have a bug in my java code...

            Comment


            • #7
              And Mce.findAllMces(); does what? A query? and what is in the fetch plan for that operation? using JDO? JPA? If using JPA you need to load the field manually before any detaching. What version?

              Comment


              • #8
                Originally posted by andy View Post
                And Mce.findAllMces(); does what? A query? and what is in the fetch plan for that operation? using JDO? JPA? If using JPA you need to load the field manually before any detaching. What version?
                This is from the *_Entity.aj file (using Roo)

                @SuppressWarnings("unchecked")
                public static List<Mce> Mce.findAllMces() {
                return entityManager().createQuery("select o from Mce o").getResultList();
                }

                It is using JPA.

                Code:
                <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
                <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
                        <provider>org.datanucleus.jpa.PersistenceProviderImpl</provider>
                        <properties>
                            <property name="datanucleus.ConnectionDriverName" value="org.hsqldb.jdbcDriver"/>
                            <property name="datanucleus.autoCreateSchema" value="false"/>
                            <property name="datanucleus.storeManagerType" value="rdbms"/>
                            <property name="datanucleus.ConnectionURL" value="jdbc:hsqldb:mem:prototype"/>
                            <property name="datanucleus.ConnectionUserName" value="sa"/>
                            <property name="datanucleus.ConnectionPassword" value=""/>
                            <property name="datanucleus.autoCreateTables" value="true"/>
                            <property name="datanucleus.autoCreateColumns" value="false"/>
                            <property name="datanucleus.autoCreateConstraints" value="false"/>
                            <property name="datanucleus.validateTables" value="false"/>
                            <property name="datanucleus.validateConstraints" value="false"/>
                            <property name="datanucleus.jpa.addClassTransformer" value="false"/>
                        </properties>
                    </persistence-unit>
                </persistence>
                I'm using Spring Roo 1.1.2.

                If using JPA you need to load the field manually before any detaching
                Could you point me to somewhere to find more information please! I thought using Spring Roo would abstract me from JPA for a simple operations as that one.

                Thank you

                Comment


                • #9
                  More interesting (to me) the version of DataNucleus.

                  Load the field manually means accessing it whilst still "attached" (managed) by the EntityManager, or mark the relation field(s) as EAGER fetching. JPA doesn't provide fetch control so thats the only control you have.

                  Comment


                  • #10
                    Hi, this is what I have.
                    datanucleus-core-1.1.5
                    datanucleus-enhancer-1.1.4
                    datanucleus-jpa-1.1.5
                    datanucleus-rdbms-1.1.5

                    I actually supposed I deed not need to do anything special.

                    This is what is generated in the IDT:

                    Code:
                    // WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
                    // You may push code into the target .java compilation unit if you wish to edit any member(s).
                    
                    package com.cristal.storm.prototype.server.domain;
                    
                    import com.cristal.storm.prototype.server.domain.Mce;
                    import java.lang.Integer;
                    import java.lang.Long;
                    import java.lang.SuppressWarnings;
                    import java.util.List;
                    import javax.persistence.Column;
                    import javax.persistence.Entity;
                    import javax.persistence.EntityManager;
                    import javax.persistence.GeneratedValue;
                    import javax.persistence.GenerationType;
                    import javax.persistence.Id;
                    import javax.persistence.PersistenceContext;
                    import javax.persistence.Version;
                    import org.springframework.transaction.annotation.Transactional;
                    
                    privileged aspect Mce_Roo_Entity {
                        
                        declare @type: Mce: @Entity;
                        
                        @PersistenceContext
                        transient EntityManager Mce.entityManager;
                        
                        @Id
                        @GeneratedValue(strategy = GenerationType.AUTO)
                        @Column(name = "id")
                        private Long Mce.id;
                        
                        @Version
                        @Column(name = "version")
                        private Integer Mce.version;
                        
                        public Long Mce.getId() {
                            return this.id;
                        }
                        
                        public void Mce.setId(Long id) {
                            this.id = id;
                        }
                        
                        public Integer Mce.getVersion() {
                            return this.version;
                        }
                        
                        public void Mce.setVersion(Integer version) {
                            this.version = version;
                        }
                        
                        @Transactional
                        public void Mce.persist() {
                            if (this.entityManager == null) this.entityManager = entityManager();
                            this.entityManager.persist(this);
                        }
                        
                        @Transactional
                        public void Mce.remove() {
                            if (this.entityManager == null) this.entityManager = entityManager();
                            if (this.entityManager.contains(this)) {
                                this.entityManager.remove(this);
                            } else {
                                Mce attached = Mce.findMce(this.id);
                                this.entityManager.remove(attached);
                            }
                        }
                        
                        @Transactional
                        public void Mce.flush() {
                            if (this.entityManager == null) this.entityManager = entityManager();
                            this.entityManager.flush();
                        }
                        
                        @Transactional
                        public void Mce.clear() {
                            if (this.entityManager == null) this.entityManager = entityManager();
                            this.entityManager.clear();
                        }
                        
                        @Transactional
                        public Mce Mce.merge() {
                            if (this.entityManager == null) this.entityManager = entityManager();
                            Mce merged = this.entityManager.merge(this);
                            this.entityManager.flush();
                            return merged;
                        }
                        
                        public static final EntityManager Mce.entityManager() {
                            EntityManager em = new Mce().entityManager;
                            if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
                            return em;
                        }
                        
                        public static long Mce.countMces() {
                            return ((Number) entityManager().createQuery("select count(o) from Mce o").getSingleResult()).longValue();
                        }
                        
                        @SuppressWarnings("unchecked")
                        public static List<Mce> Mce.findAllMces() {
                            return entityManager().createQuery("select o from Mce o").getResultList();
                        }
                        
                        public static Mce Mce.findMce(Long id) {
                            if (id == null) return null;
                            return entityManager().find(Mce.class, id);
                        }
                        
                        @SuppressWarnings("unchecked")
                        public static List<Mce> Mce.findMceEntries(int firstResult, int maxResults) {
                            return entityManager().createQuery("select o from Mce o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
                        }
                        
                    }

                    Comment


                    • #11
                      Sadly the versions of DataNucleus there are ancient. We only support 3.0 (current dev) and 2.2. Why Roo wants to use such old versions only Spring can answer.

                      As mentioned earlier, make your tags field EAGER loaded (read up on JPA). That's all can be said (from DataNucleus side).

                      Comment

                      Working...
                      X