facebook

[EJB3 Enity] How to map result set

  1. MyEclipse IDE
  2.  > 
  3. Java EE Development (EJB, JSP, Struts, XDoclet, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #273031 Reply

    Mehdi.b
    Member

    Hello !

    I’m using the MyEcplise EJB3 reverse engineering to create my Entity Bean.
    For example I have a Table named “Collab” with some properties :

    
        private CollabId id;
    
        private String exploit;
    
        private String section;
    
        private String nom;
    
        private String prenom;
    
        private String init;
    
        private String codemp;
    
        private String codLibpro;
    
        private String libprof;
    
        private Date dbtcontr;
    
        private Date fincontr;
    
        private Date sortie;
    
        private Date findroit;
    

    The CollabId class…

    
        private String type;
    
        private String identite;
    
        private String societe;
    

    For example, I just want to retrieve the “exploit” property.

    With the generation, i get some methods like this..

    
        @SuppressWarnings("unchecked")
        public List<Collab> findAll() {
            EntityManagerHelper.log("finding all Collab instances", Level.INFO,
                    null);
            try {
                String queryString = "select model from Collab model";
                return entityManager.createQuery(queryString).getResultList();
            } catch (RuntimeException re) {
                EntityManagerHelper.log("find all failed", Level.SEVERE, re);
                throw re;
            }
        }
    

    What I want is to change the query to

    String queryString = "select model.exploit from Collab model";

    But I have a “java.lang.ClassCastException: java.lang.String”. I suppose that I have to map the result set to the “Collab” entity. How can I do that ?

    Another question : Here is my persistence.xml

    
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
        version="1.0">
    
        <persistence-unit name="CRUD" transaction-type="RESOURCE_LOCAL">
    
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
    
            <!--jta-data-source>java:/DB2400DS</jta-data-source-->
            <non-jta-data-source>java:/DB2400DS</non-jta-data-source>
    
            <properties>
    
                <property name="hibernate.dialect"
                    value="org.hibernate.dialect.DB2400Dialect" />
                    
                    <property name="hibernate.show_sql"
                    value="true" />
                    
                    
                    <property name="hibernate.format_sql"
                    value="true" />
    
            </properties>
    
        </persistence-unit>
    
    </persistence>
    

    First, I can’t change my transaction-type to JTA without getting errors.

    But with RESOURCE_LOCAL transaction-type; on each request to database; I get this WARN :

    [AbstractEntityManagerImpl] Calling joinTransaction() on a non JTA EntityManager

    How can I fix this ?

    Thanks in advance !

    #273115

    Riyad Kalla
    Member

    In reference to your first question, don’t try and munge the findAll to do what you want, instead write a new method that returns you a list of Strings or something similar. The error is correct though, you are asking JPA back for a list of Strings, but you are trying to place them into a list of Collab objects. You need to either create a new method and update the list qualification or change the query.

    Also for the second question, section 4.2.1.1 should help here: http://www.hibernate.org/hib_docs/entitymanager/reference/en/html/transactions.html

    #273512

    Mehdi.b
    Member

    Thanks a lot for your answer !! I’ve fix the problem.

    But now I have an another question lol 🙂
    I would like to disable committing.
    I use now the “JTA” transaction-type. On each “save/persist” method, I’ve figured out that it makes a commit. So when you have a series of saving. It may be slowing down the system.
    How can i control commitment ?
    note : I always use Hibernate persistence provider.

    Thanks a lot for everything !!

    #273578

    Riyad Kalla
    Member

    Our generated DAOs don’t use transactions out of the box, so if you added Transactions you can remove them to get rid of this behavior, although unless you have an insanely busy site, I imagine you have a lot of other code to write first before worrying about the few miliseconds it takes to commit records to the DB 😉

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: [EJB3 Enity] How to map result set

You must be logged in to post in the forum log in