Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    JPA TreeGrid new record

    Hi.
    I have a problem with the JPA. My goal is to use a Button to add new records to a data source. I use the SmartGWT EE JPA. This is my first code, which does not work.

    If I remove the foreignKey from the tree_DataSource.DS.xml, then I can add a Record to the DataSource. But a tree without foreign key is not a tree in the view!

    Code:
    <DataSource
        ID="tree_DataSource"
        serverConstructor="com.isomorphic.jpa.JPADataSource"
        beanClassName="com.detection.smiths.eapcfg.server.persistence.Tree"
        >
        <fields>
            <field name="treeNodeId"     type="sequence" hidden="true"   primaryKey="true" />
            <field name="parentNodeID"   type="long" foreignKey="tree_DataSource.treeNodeId" title="pid" required="false"  rootValue="1" detail="false"/> 
            <field name="referenceNodeID"   type="long"     title="rTyp"    required="false"   />
            <field name="nodeTyp"   type="text"     title="Typ"    required="true"   />
            <field name="nodeName"   type="text"     title="Name"    required="true"   />
            <field name="wa"   type="text"     title="WA" required="true"   />
        </fields>
    </DataSource>
    Code:
    package com.detection.smiths.eapcfg.server.persistence;
    
    import java.io.Serializable;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table (name="Tree")
    public class Tree
        implements Serializable
    {
    
        @Id
        @Column (nullable = false)
        @GeneratedValue (strategy = GenerationType.IDENTITY)
        private Long treeNodeId;
    
        @Column (nullable = false)
        private String nodeName;
    
        @Column (nullable = false)
        private String wa;
    
        @Column (nullable = false)
        private String nodeTyp;
    
        @Column (nullable = false)
        private Long parentNodeID;
    
        @Column (nullable = false)
        private Long referenceNodeID;
    
        public Tree ()
        {
        }
    
    
        /**
         * Returns a string representation of the object. Resulting string contains
         * full name of the class and list of its properties and their values.
         *
         * @return <code>String</code> representation of this object.
         */
        @Override
        public String toString ()
        {
            return getClass().getName()
                   + "["
                   + "treeNodeId=" + ((getTreeNodeId() == null) ? "null" : getTreeNodeId().toString())
                   + ", "
                   + "parentNodeID=" + ((getParentNodeID() == null) ? "null" : getParentNodeID().toString())
                   + ", "
                   + "referenceNodeID=" + ((getReferenceNodeID() == null) ? "null" : getReferenceNodeID().toString())
                   + ", "
                   + "nodeTyp=" + ((getNodeTyp() == null) ? "null" : getNodeTyp().toString())
                   + ", "
                   + "nodeName=" + ((getNodeName() == null) ? "null" : getNodeName().toString())
                   + ", "
                   + "wa=" + ((getWa() == null) ? "null" : getWa().toString())
                   + "]";
        }
    
    	public Long getTreeNodeId() {
    		return treeNodeId;
    	}
    
    	public void setTreeNodeId(Long treeNodeId) {
    		this.treeNodeId = treeNodeId;
    	}
    
    
    	public String getNodeName() {
    		return nodeName;
    	}
    
    
    	public void setNodeName(String nodeName) {
    		this.nodeName = nodeName;
    	}
    
    
    	public String getWa() {
    		return wa;
    	}
    
    
    	public void setWa(String wa) {
    		this.wa = wa;
    	}
    
    
    	public String getNodeTyp() {
    		return nodeTyp;
    	}
    
    
    	public void setNodeTyp(String nodeTyp) {
    		this.nodeTyp = nodeTyp;
    	}
    
    
    	public Long getParentNodeID() {
    		return parentNodeID;
    	}
    
    
    	public void setParentNodeID(Long parentNodeID) {
    		this.parentNodeID = parentNodeID;
    	}
    
    
    	public Long getReferenceNodeID() {
    		return referenceNodeID;
    	}
    
    
    	public void setReferenceNodeID(Long referenceNodeID) {
    		this.referenceNodeID = referenceNodeID;
    	}
    
    }
    Code:
    	final DataSource treeDS = DataSource.get("tree_DataSource");
    
            IButton addButton = new IButton("add record");
            addButton.addClickHandler(new ClickHandler()
            {
                public void onClick (ClickEvent event)
                {
                	Record r=new Record();
                	r.setAttribute("nodeName", "DE");
                	r.setAttribute("parentNodeID", 1);
                	r.setAttribute("referenceNodeID", 1);
                	r.setAttribute("nodeTyp", "ABC");
                	r.setAttribute("wa", "new WA");
                	treeDS.addData(r);
                	
                }
            });
    Code:
     <persistence-unit name="ds" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
          ....
        <class>com.detection.smiths.eapcfg.server.persistence.Tree</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
          <property name="hibernate.hbm2ddl.auto" value="create-drop" />
           ...
        </properties>
      </persistence-unit>

    #2
    Hi,

    First: there is no type="long" - use type="integer".

    Now to relations:
    To make JPA relations work you have to define relation at JPA level (in your entity declaration):
    Code:
    // Replace these lines
    //    @Column (nullable = false)
    //    private Long parentNodeID;
    // with
        @ManyToOne
        @JoinColumn(name="parentNodeID", referencedColumnName="treeNodeId")
        private Tree parentNodeID;
    Update getter/setter type from Long to Tree as well.

    Regards,
    Alius
    Last edited by alius; 7 Jun 2012, 15:23.

    Comment

    Working...
    X