Announcement

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

    Persisting a colllection with jpa datasource problem

    I try to persist nested records with jpa datasources, but getting warning "Can't convert value of type java.lang.Long to target type". It seems it doesen't understand the ManyToOne field when it is in a nested record.

    In the sample code, when you press The Insert button, the Order will be persisted but the OrderItem not. (If I remove completly the 'product' field from OrderItem and from datasource, the Orderitem persisted correctly.)

    Version: SmartGWT 4.1p build: 07/01/2014 Eval

    Order.java
    Code:
    @Entity
    public class Order {
    	
    	@Id
    	@GeneratedValue
    	private Integer id;
    	
    	private String name;
    	
    	@OneToMany(cascade={CascadeType.ALL})
    	@JoinColumn(name="order_id")
    	private Set<OrderItem> orderItems = new HashSet<OrderItem>();
    
    // setters, getters ...
    }
    OrderItem.java
    Code:
    @Entity
    public class OrderItem {
    	
    	@Id
    	@GeneratedValue
    	private Integer id;
    	
    	private Integer amount;
    	
    	@ManyToOne
    	@JoinColumn(name="product_id")
    	private Product product;
    
    // setters, getters ...	
    }
    Product.java
    Code:
    @Entity
    public class Product {
    
    	@Id
    	@GeneratedValue
    	private Integer id;
    	
    	private String name;
    
    // setters, getters ...	
    }
    order.ds.xml
    Code:
    <DataSource  
        ID="order"  
        serverType="jpa"  
        autoDeriveSchema="true"  
        schemaBean="com.pt.aop.model.test.Order"
    >
        <fields>  
            <field name="id"/>
            <field name="name" required="true"/>
            <field name="orderItems" type="order_item" multiple="true" foreignKey="order_item.id" hidden="true"/>
        </fields>
    </DataSource>
    order_item.ds.xml
    Code:
    <DataSource  
        ID="order_item"  
        serverType="jpa"  
        autoDeriveSchema="true"  
        schemaBean="com.pt.aop.model.test.OrderItem"
    >
        <fields>  
            <field name="id"/>
            <field name="amount"/>
            <field name="product" required="true" foreignKey="product.id" editorType="SelectItem" displayField="name"/>
        </fields>
    </DataSource>
    product.ds.xml
    Code:
    <DataSource  
        ID="product"  
        serverType="jpa"  
        autoDeriveSchema="true"  
        schemaBean="com.pt.aop.model.test.Product"
    >
    	<fields>
    		<field name="id"/>
        	<field name="name" required="true"/>
        </fields>
    </DataSource>
    Test code
    In the product table there must be a row with primary key '1'.
    Code:
    public class _Test implements EntryPoint {
    
    	public void onModuleLoad() {
    		HLayout layout = new HLayout(20);
    		
    		
    		final Record orderRecord = new Record();
    		orderRecord.setAttribute("name", "First order");
    		
    		Record orderItemRecord = new Record();
    		orderItemRecord.setAttribute("amount", 30);
    		orderItemRecord.setAttribute("product", 1);
    		
    		Record[] orderItemRecords = {orderItemRecord};
    		
    		orderRecord.setAttribute("orderItems", orderItemRecords);
    		
    		Button button = new Button("Insert Record");
    		
    		button.addClickHandler(new ClickHandler() {
    			@Override
    			public void onClick(ClickEvent event) {
    				DataSource orderDS = DataSourceFactory.getInstance("order");
    				orderDS.addData(orderRecord);
    			}
    		});
    		
    		layout.addMember(button);
    
    		layout.draw();
    	}
    }
    Server log:
    === 2014-10-22 16:49:27,266 [ec-9] INFO RequestContext - URL: '/master/app/sc/IDACall', User-Agent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36': Safari with Accept-Encoding header
    === 2014-10-22 16:49:27,275 [ec-9] DEBUG XML - Parsed XML from (in memory stream): 7ms
    === 2014-10-22 16:49:27,275 [ec-9] DEBUG ISCKeyedObjectPool - Borrowing object for 'transaction'
    === 2014-10-22 16:49:27,277 [ec-9] DEBUG ISCKeyedObjectPool - Borrowing object for 'Object'
    === 2014-10-22 16:49:27,279 [ec-9] DEBUG ISCKeyedObjectPool - Borrowing object for 'List'
    === 2014-10-22 16:49:27,280 [ec-9] DEBUG ISCKeyedObjectPool - Borrowing object for 'elem'
    === 2014-10-22 16:49:27,284 [ec-9] DEBUG RPCManager - Processing 1 requests.
    === 2014-10-22 16:49:27,289 [ec-9] DEBUG ISCKeyedObjectPool - Borrowing object for 'order'
    === 2014-10-22 16:49:27,290 [ec-9] DEBUG RPCManager - Request #1 (DSRequest) payload: {
    values:{
    name:"First order",
    orderItems:[
    {
    amount:30,
    product:1
    }
    ]
    },
    operationConfig:{
    dataSource:"order",
    operationType:"add",
    textMatchStyle:"exact"
    },
    appID:"builtinApplication",
    operation:"order_add",
    oldValues:{
    name:"First order",
    orderItems:[
    {
    amount:30,
    product:1
    }
    ]
    },
    criteria:{
    }
    }
    === 2014-10-22 16:49:27,291 [ec-9] DEBUG DeclarativeSecurity - Processing security checks for DataSource null, field null
    === 2014-10-22 16:49:27,291 [ec-9] DEBUG DeclarativeSecurity - DataSource order is not in the pre-checked list, processing...
    === 2014-10-22 16:49:27,291 [ec-9] DEBUG AppBase - [builtinApplication.order_add] No userTypes defined, allowing anyone access to all operations for this application
    === 2014-10-22 16:49:27,292 [ec-9] DEBUG AppBase - [builtinApplication.order_add] No public zero-argument method named '_order_add' found, performing generic datasource operation
    === 2014-10-22 16:49:27,292 [ec-9] DEBUG JPADataSource - [builtinApplication.order_add] Auto-joining transactions.
    === 2014-10-22 16:49:27,292 [ec-9] DEBUG JPADataSource - [builtinApplication.order_add] Creating EntityManager, starting transaction and setting it to RPCManager.
    === 2014-10-22 16:49:27,325 [ec-9] DEBUG DSRequest - [builtinApplication.order_add] Clobbering existing FreeResourcesHandler of type 'com.isomorphic.jpa.JPA2DataSource' with a 'com.isomorphic.jpa.JPA2DataSource'
    === 2014-10-22 16:49:27,325 [ec-9] DEBUG ISCKeyedObjectPool - [builtinApplication.order_add] Borrowing object for 'order_item'
    === 2014-10-22 16:49:27,326 [ec-9] WARN JPADataSource - [builtinApplication.order_add] null field passed.
    === 2014-10-22 16:49:27,326 [ec-9] WARN JPADataSource - [builtinApplication.order_add] null field passed.
    === 2014-10-22 16:49:27,326 [ec-9] DEBUG JPADataSource - [builtinApplication.order_add] Executing add.
    === 2014-10-22 16:49:27,327 [ec-9] DEBUG ISCKeyedObjectPool - [builtinApplication.order_add] Borrowing object for 'order_item'
    === 2014-10-22 16:49:27,328 [ec-9] DEBUG JPA2DSGenerator - [builtinApplication.order_add] Instantiating class com.pt.aop.model.test.OrderItem
    === 2014-10-22 16:49:27,328 [ec-9] DEBUG JPA2DSGenerator - [builtinApplication.order_add] Generating data source 'OrderItem_JPA' for class com.pt.aop.model.test.OrderItem
    === 2014-10-22 16:49:27,328 [ec-9] DEBUG JPA2DSGenerator - [builtinApplication.order_add] Generating field 'id' of type java.lang.Integer
    === 2014-10-22 16:49:27,329 [ec-9] DEBUG JPA2DSGenerator - [builtinApplication.order_add] Singular field configuration generated: {hidden=true, primaryKey=true, name=id, required=false, canEdit=false, type=sequence}
    === 2014-10-22 16:49:27,329 [ec-9] DEBUG JPA2DSGenerator - [builtinApplication.order_add] Generating field 'amount' of type java.lang.Integer
    === 2014-10-22 16:49:27,330 [ec-9] DEBUG JPA2DSGenerator - [builtinApplication.order_add] Singular field configuration generated: {name=amount, required=false, type=integer}
    === 2014-10-22 16:49:27,330 [ec-9] DEBUG JPA2DSGenerator - [builtinApplication.order_add] Generating field 'product' of type com.pt.aop.model.test.Product
    === 2014-10-22 16:49:27,330 [ec-9] DEBUG JPA2DSGenerator - [builtinApplication.order_add] Property with reference to another entity skipped: {name=product, type=any}
    === 2014-10-22 16:49:27,331 [ec-9] DEBUG ISCKeyedObjectPool - [builtinApplication.order_add] Borrowing object for 'product'
    Hibernate:
    select
    orderitem0_.id as id89_,
    orderitem0_.amount as amount89_,
    orderitem0_.product_id as product3_89_
    from
    order_items orderitem0_
    where
    orderitem0_.id=?
    === 2014-10-22 16:49:27,394 [ec-9] WARN DataSource - [builtinApplication.order_add] Couldn't set property 'orderItems' for datasource 'order'. Actual error: java.lang.IllegalArgumentException: Can't convert value of type java.lang.Long to target type com.pt.aop.model.test.Product
    === 2014-10-22 16:49:27,394 [ec-9] DEBUG ISCKeyedObjectPool - [builtinApplication.order_add] Borrowing object for 'text'
    === 2014-10-22 16:49:27,395 [ec-9] DEBUG ISCKeyedObjectPool - [builtinApplication.order_add] Borrowing object for 'text'
    Hibernate:
    insert
    into
    orders
    (name)
    values
    (?)
    === 2014-10-22 16:49:27,418 [ec-9] WARN DSResponse - [builtinApplication.order_add] Attempted to call getRecord() on a DSResponse with null DataSource
    === 2014-10-22 16:49:27,418 [ec-9] WARN BasicDataSource - [builtinApplication.order_add] There are no records to be transformed by transformMultipleFields!
    === 2014-10-22 16:49:27,419 [ec-9] DEBUG RPCManager - Content type for RPC transaction: text/plain; charset=UTF-8
    === 2014-10-22 16:49:27,419 [ec-9] DEBUG JPADataSource - Committing transaction for 1 queued operation(s).
    === 2014-10-22 16:49:27,428 [ec-9] DEBUG RPCManager - non-DMI response, dropExtraFields: false
    === 2014-10-22 16:49:27,429 [ec-9] DEBUG ISCKeyedObjectPool - Borrowing object for 'order_item'
    === 2014-10-22 16:49:27,430 [ec-9] DEBUG JPA2DSGenerator - Instantiating class com.pt.aop.model.test.OrderItem
    === 2014-10-22 16:49:27,430 [ec-9] DEBUG JPA2DSGenerator - Generating data source 'OrderItem_JPA' for class com.pt.aop.model.test.OrderItem
    === 2014-10-22 16:49:27,430 [ec-9] DEBUG JPA2DSGenerator - Generating field 'id' of type java.lang.Integer
    === 2014-10-22 16:49:27,430 [ec-9] DEBUG JPA2DSGenerator - Singular field configuration generated: {hidden=true, primaryKey=true, name=id, required=false, canEdit=false, type=sequence}
    === 2014-10-22 16:49:27,430 [ec-9] DEBUG JPA2DSGenerator - Generating field 'amount' of type java.lang.Integer
    === 2014-10-22 16:49:27,431 [ec-9] DEBUG JPA2DSGenerator - Singular field configuration generated: {name=amount, required=false, type=integer}
    === 2014-10-22 16:49:27,431 [ec-9] DEBUG JPA2DSGenerator - Generating field 'product' of type com.pt.aop.model.test.Product
    === 2014-10-22 16:49:27,431 [ec-9] DEBUG JPA2DSGenerator - Property with reference to another entity skipped: {name=product, type=any}
    === 2014-10-22 16:49:27,431 [ec-9] DEBUG ISCKeyedObjectPool - Borrowing object for 'product'
    === 2014-10-22 16:49:27,432 [ec-9] DEBUG JPA2DSGenerator - Instantiating class com.pt.aop.model.test.Product
    === 2014-10-22 16:49:27,432 [ec-9] DEBUG JPA2DSGenerator - Generating data source 'Product_JPA' for class com.pt.aop.model.test.Product
    === 2014-10-22 16:49:27,432 [ec-9] DEBUG JPA2DSGenerator - Generating field 'id' of type java.lang.Integer
    === 2014-10-22 16:49:27,432 [ec-9] DEBUG JPA2DSGenerator - Singular field configuration generated: {hidden=true, primaryKey=true, name=id, required=false, canEdit=false, type=sequence}
    === 2014-10-22 16:49:27,433 [ec-9] DEBUG JPA2DSGenerator - Generating field 'name' of type java.lang.String
    === 2014-10-22 16:49:27,433 [ec-9] DEBUG JPA2DSGenerator - Singular field configuration generated: {name=name, required=false, type=text}
    === 2014-10-22 16:49:27,434 [ec-9] DEBUG JPADataSource - Releasing entity manager.

    #2
    Have you already looked over the JPA / Hibernate Relations overview in the docs? It explains how to declare each type of relationship.

    Comment


      #3
      I've found the bug: Product class is missing constructors.

      Code:
             public Product(Long id) {
      		this.id = id.intValue();
             }
      	
             public Product() {
             }

      Comment

      Working...
      X