Announcement

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

    JPA datasource/mapping problem

    Hi,
    I'm evaluating SmartGWTEE 2.4 at the moment and I have a question concerning the JPA datasource handling.

    I have a tree mapping using the @OneToMany and @ManyToOne annotations.
    The entity class could look something like that:

    Code:
    @Entity
    public class MyTreeNode implements Serializable {
    
        private Long id;
        private String name;
        private MyTreeNode parent;
        private List<MyTreeNode> children;
    
        @Id
        @GeneratedValue
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @ManyToOne
        public MyTreeNode getParent() {
            return parent;
        }
    
        public void setParent(MyTreeNode parent) {
            this.parent = parent;
        }
    
        @OneToMany(cascade = CascadeType.ALL)
        public List<MyTreeNode> getChildren() {
            return children;
        }
    
        public void setChildren(List<MyTreeNode> children) {
            this.children = children;
        }
    }
    The datasource
    Code:
    <DataSource ID="tree_DS" serverConstructor="com.isomorphic.jpa.JPADataSource" beanClassName="com.sample.server.MyTreeNode">
        <fields>
            <field name="id" type="integer" hidden="true" primaryKey="true"/>
            <field name="name" type="text" title="Name" required="true"/>
            <field name="parent" type="integer" foreignKey="tree_DS.id"/>
        </fields>
    </DataSource>
    Now, if I want to create a new Treenode resp. Record on the client I only have the IDs of my entities available.
    So if I do something like that:

    Code:
    button.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent clickEvent) {
                    com.smartgwt.client.widgets.tree.TreeNode selectedRecord = (com.smartgwt.client.widgets.tree.TreeNode) treeGrid.getSelectedRecord();
                    String parentId = selectedRecord == null ? "" : selectedRecord.getAttribute("id");
                    com.smartgwt.client.widgets.tree.TreeNode node = new com.smartgwt.client.widgets.tree.TreeNode();
                    node.setAttribute("name", "SampleName");
                    node.setAttribute("parent", parentId);
                    treeGrid.addData(node);
                }
            });
    I get the obvious error "Can't convert value of type java.lang.Long to target type com.sample.server.MyTreeNode"


    Here's what the request looks like:
    Code:
    {
        values:{
            name:"SampleName",
            parent:"1"
        },
        operationConfig:{
            dataSource:"tree_DS",
            operationType:"add"
        },
        componentId:"isc_TreeGrid_0",
        appID:"builtinApplication",
        operation:"tree_DS_add",
        oldValues:{
            name:"SampleName",
            parent:"1"
        },
        criteria:{
        }
    }
    Am I missing something here? Or do I have to use DMI and set the Parent entity manually?

    Thanks in advance.
    Last edited by doit; 30 Mar 2011, 00:56.

    #2
    After looking at the ds-jpa sample project I've noticed some differences in the mapping and I think I've got it working now.

    The bold passages were edited.
    MyTreeNode.java
    Code:
    @Entity
    public class MyTreeNode implements Serializable {
    
        private Long id;
        private String name;
        private MyTreeNode parent;
        private List<MyTreeNode> children;
        [b]private Long parentId;[/b]
    
        @Id
        @GeneratedValue
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @ManyToOne
       [b] @JoinColumn(name = "parentId", referencedColumnName = "parentId", nullable=false, insertable=false, updatable=false)[/b]
        public MyTreeNode getParent() {
            return parent;
        }
    
        public void setParent(MyTreeNode parent) {
            this.parent = parent;
        }
    
        @OneToMany(cascade = CascadeType.ALL)
        public List<MyTreeNode> getChildren() {
            return children;
        }
    
        public void setChildren(List<MyTreeNode> children) {
            this.children = children;
        }
    [b]
        public Long getParentId() {
            return parentId;
        }
    
        public void setParentId(Long parentId) {
            this.parentId = parentId;
        }[/b]
    }
    tree_DS.ds.xml
    Code:
    <DataSource ID="tree_DS" serverConstructor="com.isomorphic.jpa.JPADataSource" beanClassName="com.sample.server.MyTreeNode">
        <fields>
            <field name="id" type="integer" hidden="true" primaryKey="true"/>
            <field name="name" type="text" title="Name" required="true"/>
            <field name=[b]"parentId"[/b] type="integer" foreignKey="tree_DS.id"/>
        </fields>
    </DataSource>
    Client-side call:
    Code:
    button.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent clickEvent) {
                    com.smartgwt.client.widgets.tree.TreeNode selectedRecord = (com.smartgwt.client.widgets.tree.TreeNode) treeGrid.getSelectedRecord();
                    String parentId = selectedRecord == null ? "" : selectedRecord.getAttribute("id");
                    com.smartgwt.client.widgets.tree.TreeNode node = new com.smartgwt.client.widgets.tree.TreeNode();
                    node.setAttribute("name", "SampleName");
                    node.setAttribute([b]"parentId"[/b], parentId);
                    treeGrid.addData(node);
                }
            });
    I still have to take a closer look at it to fully understand what's going on there, but at least it's working for now. :)

    Regards

    Comment

    Working...
    X