Announcement Announcement Module
Collapse
No announcement yet.
Unable to use Entity created using Spring Roo Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to use Entity created using Spring Roo

    I created Entity using Spring Roo:

    Code:
    project --topLevelPackage com
    jpa setup --provider HIBERNATE --database HYPERSONIC_PERSISTENT  
    entity jpa --class ~.Timer --testAutomatically
    field string --fieldName message --notNull
    I then use mvn install to package the result to data.jar

    Next, I create an Eclipse project and set dependency to the data.jar and try to use it as follow:

    Code:
    public class App {
    public static void main(String[] args) {
        FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext(
        "D:/Proj/applicationContext.xml");
        Timer t = new Timer();
        t.setMessage("msg");
        Timer.entityManager().persist(t);
    }
    }
    Spring config:

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    <context:spring-configured/>
    <context:component-scan base-package="com">
        <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>
    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
        <property name="url" value="jdbc:hsqldb:file:d:/tmp/man/com;shutdown=true"/>
        <property name="username" value="usr"/>
        <property name="password" value="pwd"/>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
        <property name="testWhileIdle" value="true"/>
        <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
        <property name="numTestsPerEvictionRun" value="3"/>
        <property name="minEvictableIdleTimeMillis" value="1800000"/>
    </bean>
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>
    </beans>
    but I get error:

    Code:
    Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: com.Timer
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:868)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
    at $Proxy30.persist(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
    at $Proxy29.persist(Unknown Source)
    at com.App.main(App.java:17)
    I need to create DAOs from command line (The reason I use Spring Roo) so I can use it on my project, stand-alone application with no Spring Roo. I can't see how Spring Roo can allow it.

    Removing Spring Roo may solve my problem but it is not an option because it involves manual task to click button on Eclipse.

    Any help to solve the issue greatly appreciated. Thanks.

  • #2
    Create from Roo a multi module project with entities in a submodule.

    Then, analyze how Spring Roo is configuring the submodule into the parent module.

    Regards !

    Comment


    • #3
      Brilliant, Spring Roo rocks, you rock.

      Just for everyone's benefit, I use the following standalone test code within mvc:
      package com.example.petclinic.ui.mvc;
      import java.util.*;
      import org.springframework.context.support.ClassPathXmlAp plicationContext;
      public class MyTest {
      public static void main(String[] args) throws Exception {
      ClassPathXmlApplicationContext context =
      new ClassPathXmlApplicationContext("classpath*:META-INF/spring/applicationContext*.xml");

      for (long i=0; i<10; i++) {
      Owner owner = new Owner();
      owner.setId(i);
      owner.setAddress("arg0");
      owner.setCity("arg0");
      owner.setLastName("arg0");
      owner.setTelephone("arg0");
      owner.setBirthDay(new Date());
      owner.persist();
      }

      System.out.println("Total owners:"+Owner.countOwners());
      }

      }

      To test this, you need to run the above once then before second run you need to change core's persistense.xml:
      <!--<property name="hibernate.hbm2ddl.auto" value="update"/>-->
      and database.properties (if not run as server data will be lost when MyTest ends):
      database.url=jdbc\:hsqldb\:hsql\://localhost\:9001/xdb
      you run your hsqldb server: c:\hsqldb\bin>runServer.bat --database.0 file:d:/tmp/data/mydb --dbname.0 xdb

      Comment

      Working...
      X