Named queries

Discuss the EJB cartridge here

Named queries

Postby chawax » Fri 20. Jul 2007, 15:53

Hi,



I noticed that named queries modeled with UML are generated as annotations in entity classes. For example :



Code: Select all
@javax.persistence.Entity
@javax.persistence.Table(name = "EMPLOYE")
// Uncomment to enable entity listener for Employe
// @javax.persistence.EntityListeners({fr.horoquartz.t4.core.employe.EmployeListener.class})
// Uncomment to enable caching for Employe
// @org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.TRANSACTIONAL)
@javax.persistence.NamedQueries
({
    @javax.persistence.NamedQuery(name = "Employe.findAll", query = "select employe from Employe AS employe"),
    @javax.persistence.NamedQuery(name = "Employe.findByMatricule", query = "from Employe as employe where employe.matricule = :matricule"),
    @javax.persistence.NamedQuery(name = "Employe.findByPrenom", query = "from Employe as employe where employe.prenom = :prenom"),
    @javax.persistence.NamedQuery(name = "Employe.findByPays", query = "from Employe as employe where employe.pays = :pays")
})
public class Employe
    extends fr.horoquartz.t4.core.employe.EmployeEmbeddable
    implements java.io.Serializable, Comparable<Employe>
{

...




But since these classes are generated in the core/src/main/java directory, which is written once, does it mean you have to change the query in both UML model and Java sources if you change a query ?



I am a newbie with EJB3, so maybe this is not a serious issue, but please tell me :wink:
chawax
Experienced Member
 
Posts: 491
Joined: Thu 7. Dec 2006, 11:38
Location: Nantes, France

Postby vance.karimi » Sun 22. Jul 2007, 05:06

You are right. Unfortunately, for entities which you have modeled operations in and as a result have an embeddable super class, this is the case. Thankfully, you shouldn't have too many of these types of hierarchies anyway.



We don't yet define entities in the orm.xml. Once we do, defining queries should become easier. I don't know when that would be though.
Vance Karimi - vance [at] andromda [dot] org

http://www.andromda.org
vance.karimi
AndroMDA Developer
 
Posts: 479
Joined: Fri 18. Nov 2005, 02:31
Location: Perth, Australia

Postby chawax » Thu 30. Oct 2008, 17:26

More than one year later, I am trying to move named queries to orm.xml :o What I try to do is just move annotations at entity level (table name, unique constraints, named queries, ...) and keep column definitions in Java classes with annotations. For what I read, this is something you can do according to JPA specification.



But I encounter a serious problem, maybe a bug with Hibernate (I asked them about this on Hibernate forum).



For example, in orm.xml I have the following entity :



Code: Select all
<entity class="t4.core.utils.process.internal.ProcessTemplate">
    <table name="HQPRC_TEMPLATE"></table>
    <named-query name="ProcessTemplate.findAll">
        <query><![CDATA[select processTemplate from ProcessTemplate AS processTemplate]]></query>
    </named-query>
    <named-query name="ProcessTemplate.findByCode">
        <query><![CDATA[from ProcessTemplate as processTemplate where processTemplate.code = :code]]></query>
    </named-query>
</entity>




Hibernate looks up column definitions in ProcessTemplate class. And only there ! It means in the case of an entity with an embeddable class, Hibernate will not look for annotations in ProcessTemplateEmbeddable :? It looks like a Hibernate bug to me, but I am waiting answer of Hibernate team about this point. The topic on Hibernate forum is there : http://forum.hibernate.org/viewtopic.php?t=991976
chawax
Experienced Member
 
Posts: 491
Joined: Thu 7. Dec 2006, 11:38
Location: Nantes, France

Postby chawax » Fri 31. Oct 2008, 11:04

How stupid I am ... I had removed @javax.persistence.MappedSuperclass annotation on entity embeddable class :o It works much better now.
chawax
Experienced Member
 
Posts: 491
Joined: Thu 7. Dec 2006, 11:38
Location: Nantes, France

Postby chawax » Mon 3. Nov 2008, 11:43

I finished it and it works, no more JPA annotations in src/main/java classes :D As I said earlier, all JPA annotations related to entities (i.e. table name, unique constraints, named queries, ...) are moved to orm.xml. Annotations to define columns are still in generated classes.



I added a namespace property, jpaMappingFile, to give the possibility to set a specific name for orm mapping file name (default is META-INF/orm.xml). This namespace property is used in mapping-file tag of persistence.xml file. Also added a merge point in persistence.xml file so that you can add some extra mapping files (you need it if you have a reference to a jar file containing entities but whom mapping file is not called orm.xml).



I filed a JIRA for this and I will supply a patch for this. Take care when applying this patch because I made a few changes in EJB3 cartridge for my specific needs, so the patch may not be applicable as it is :wink:



http://galaxy.andromda.org/jira/browse/EJB-113
chawax
Experienced Member
 
Posts: 491
Joined: Thu 7. Dec 2006, 11:38
Location: Nantes, France


Return to EJB cartridge

Who is online

Users browsing this forum: No registered users and 0 guests