Tagged: ejb Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 5:56 pm on October 18, 2010 Permalink |
    Tags: , DBMS, ejb, ejb3, entity, hibernate, , ORM, query, , session   

    How-to: Write a named query for an entity bean with composite key 

    I was working on an EJB3 project where I had to write an entity bean with a composite primary key. And I needed to write a named query to which had to use a sub set of the composite key. And this is what I did.

    The entity bean and the composite key:

    import javax.persistence.Column;
    import javax.persistence.EmbeddedId;
    import javax.persistence.Entity;
    import javax.persistence.NamedQueries;
    import javax.persistence.NamedQuery;
    import javax.persistence.Table;
    
    @Entity
    @Table (name = "myTable")
    public class MyEntityBean implements Serializable
    {
        @EmbeddedId
        private MyCompositeKey key;
    
        // Other instance members
    
        @Column (name = "value")
        private String value;
    
        // Getters & setters
        ...
    }
    
    import java.io.Serializable;
    import javax.persistence.Column;
    import javax.persistence.Embeddable;
    
    @Embeddable
    public class MyCompositeKey implements Serializable
    {
        @Column (name = "key1", nullable = false)
        private String key1;
    
        @Column (name = "key2", nullable = false)
        private String key2;
    
        // Getters & setters
        ....
    }
    

    The equivalent database table will look like:

    +---------------+--------------+------+-----+---------+-------+
    | Field         | Type         | Null | Key | Default | Extra |
    +---------------+--------------+------+-----+---------+-------+
    | key1          | varchar(255) | NO   | PRI |         |       |
    | key2          | varchar(255) | NO   | PRI |         |       |
    | value         | varchar(255) | YES  |     |         |       |
    +---------------+--------------+------+-----+---------+-------+
    

    Writing a named query which makes use of the elements of the composite key is a li’l tricky. Take a look at the following SQL statement which tries to fetch all the records from the table for a matching key1.

    SELECT * FROM myTable WHERE key1='some value';
    

    Embedding the named query in MyEntityBean.

    @Entity
    @Table (name = "myTable")
    @NamedQueries
    ({
        @NamedQuery(
            name = "myQuery",
            query = "SELECT FROM MyEntityBean b WHERE key.key1 = :key1" )
    })
    public class MyEntityBean implements Serializable
    {
        ...
    }
    

    We should be careful about the use of the variable name in the named query. In the named query, we are supposed to use the name of the composite key exactly the same as what we have specified in the bean.

    This query can be accessed from a session bean with the help of EntityManager.

    import javax.ejb.Remote;
    import javax.ejb.Stateless;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.Query;
    
    @Remote
    @Stateless (name = "MySessionBean")
    public class MySessionBeanImpl implements SessionBean
    {
        @PersistenceContext
        private EntityManager entityManager;
    
        public void myMethod(String key)
        {
        	Query aQuery = null;
    
        	aQuery = this.entityManager.createNamedQuery("myQuery");
            aQuery.setParameter("key1", key);
    
            List resultList = aQuery.getResultList();
    
            .....
        }
    }
    

    Hope this helps. Have a good day 🙂

    Advertisements
     
  • Subinkrishna Gopi 4:22 pm on February 11, 2009 Permalink |
    Tags: ejb, , , , migration, , , ,   

    Another JBoss 5 hack by Venu 

    jbosscorp_logo

    Venu had another JBoss hack and he was able to fix one more issue that may bubble up during the migration from JBoss 4.x to 5. This time found out how solve “Adding multiple last resources is disallowed. Current resource is ….” exception which happens while trying to access an EJB from a different EAR with in a transaction and trying to commit that transaction.

    Read the article here

     
  • Subinkrishna Gopi 5:02 pm on January 22, 2009 Permalink |
    Tags: , ejb, , , , , ,   

    A Jboss 5 issue 

    jbosscorp_logo

    Me and my colleague Venu were working on some Jboss 5 stuff  (infact migrating from Jboss 4 to 5) when we got the following exception.

    Exception:

    ERROR [my.package.name] doSomething() Ex: javax.naming.CommunicationException [Root exception is java.io.InvalidClassException: org.jboss.ejb3.remoting.IsLocalInterceptor; local class incompatible: stream classdesc serialVersionUID = -3758782076801249473, local class serialVersionUID = 337700910587744646]

    javax.naming.CommunicationException [Root exception is java.io.InvalidClassException: org.jboss.ejb3.remoting.IsLocalInterceptor; local class incompatible: stream classdesc serialVersionUID = -3758782076801249473, local class serialVersionUID = 337700910587744646]

    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:725)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:590)
    at javax.naming.InitialContext.lookup(InitialContext.java:351)

    .. .. ..

    Caused by: java.io.InvalidClassException: org.jboss.ejb3.remoting.IsLocalInterceptor; local class incompatible: stream classdesc serialVersionUID = -3758782076801249473, local class serialVersionUID = 337700910587744646

    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:546)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1552)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1466)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1699)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1305)

    We are trying to connect to another server which is running on Jboss 4 and trying to access a remote EJB (session bean). Till now I’ve no clear idea about this whole issue. If any of you can put some light to this issue, please.

    I will update this place once we tackle the issue.

    Update 1:
    I found some useful information here: https://jira.jboss.org/jira/browse/EJBTHREE-749

    The problem seems to be mismatching SerialVersionUID in org.jboss.ejb3.remoting.IsLocalInterceptor of  jboss-ejb3-client.jar.

    Update 2: 28-Jan-2009
    This issue has been fixed from Jboss-4.2.2 GA onwards. So all those who are using versions lesser than 4.2.2 are more likely to get this issue.

    Find the JIRA post by Venu here: https://jira.jboss.org/jira/browse/EJBTHREE-1696

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel