[hibernate-issues] [Hibernate-JIRA] Updated: (ANN-593) Ability for an @NamedQuery on a @MappedSuperclass to be copied up to the subclass

John Newman (JIRA) noreply at atlassian.com
Mon Apr 9 10:14:06 EDT 2007


     [ http://opensource.atlassian.com/projects/hibernate/browse/ANN-593?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

John Newman updated ANN-593:
----------------------------

    Attachment: TestUserService.java

Yes that's exactly what is happening.  However, I'm not sure if we can make this happen.  Even if we could, I'm not sure how practical it would be to use.   Consider this,

superclass query:  @NamedQuery(name="OneDistinctSubclassName.findBySomething", query="FROM OneDistinctSublcassName where something = ?")

If my subclass isn't called "OneDistinctSubclassName" it doesn't work.  So this would only work if you extend a class once and give it a common name in a project.  This is still useful if you have a class that is extended once in multiple projects (what i'm doing in the original example).  thoughts?

Anyway I've attached a test case, you can see from the logs below that the query in the superclass doesn't get bound, and it blows up when i try to pull it out of the session.  Queries on the subclass work fine.

[code]
....

09:57:34,733  INFO EntityBinder:378 - Bind entity edu.upmc.ccweb.dosimetry.model.Machine on table tbl_machine
09:57:34,749  INFO AnnotationBinder:388 - Binding entity from annotated class: edu.upmc.ccweb.dosimetry.hibtest.User
09:57:34,749  INFO QueryBinder:233 - Binding named query: User.findByFirstName => FROM User WHERE firstName = :firstName
09:57:34,749  INFO EntityBinder:378 - Bind entity edu.upmc.ccweb.dosimetry.hibtest.User on table User
09:57:34,764  INFO AnnotationBinder:388 - Binding entity from annotated class: edu.upmc.ccweb.dosimetry.model.Patient

....

Hibernate:
    select
        user0_.id as id6_,
        user0_.loginName as loginName6_,
        user0_.firstName as firstName6_,
        user0_.lastName as lastName6_
    from
        User user0_
    where
        user0_.firstName=?
Creating C:\projects\dosimetry\test-output\dosimetry\edu.upmc.ccweb.dosimetry.hibtest.TestUserService.html
PASSED: findByFirstName
FAILED: findByLoginName
org.hibernate.MappingException: Named query not known: User.findByLoginName
	at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:70)
	at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1260)
	at edu.upmc.ccweb.dosimetry.hibtest.UserService.findByLoginName(UserService.java:25)
	at edu.upmc.ccweb.dosimetry.hibtest.TestUserService.findByLoginName(TestUserService.java:39)
... Removed 17 stack frames

===============================================
    edu.upmc.ccweb.dosimetry.hibtest.TestUserService
    Tests run: 2, Failures: 1, Skips: 0
===============================================[/code]

> Ability for an @NamedQuery on a @MappedSuperclass to be copied up to the subclass
> ---------------------------------------------------------------------------------
>
>                 Key: ANN-593
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/ANN-593
>             Project: Hibernate Annotations
>          Issue Type: Improvement
>          Components: binder
>    Affects Versions: 3.2.1
>         Environment: INFO Version:15 - Hibernate Annotations 3.2.1.GA
> 09:15:01,755  INFO Environment:500 - Hibernate 3.2.1
> INFO SettingsFactory:81 - RDBMS: Microsoft SQL Server, version: 08.00.0760
> 09:15:06,114  INFO SettingsFactory:82 - JDBC driver: jTDS Type 4 JDBC Driver for MS SQL Server and Sybase, version: 1.2
> 09:15:06,255  INFO Dialect:151 - Using dialect: org.hibernate.dialect.SQLServerDialect
>            Reporter: John Newman
>         Attachments: TestUserService.java, UserService.java
>
>   Original Estimate: 4 hours
>  Remaining Estimate: 4 hours
>
> If I use a @MappedSuperclass (which is a very useful annotation btw) I have to manually copy any queries I want with taht class up to all subclasses.  The binder should check any mapped superclasses and pull those named queries up.  i.e.
> /**
>  * Base User class that different projects user classes derive from. 
>  * Hibernate annotations doesn't pick up that query, so you have to copy it up to your subclass
>  */
> @MappedSuperclass
> @NamedQueries({
> 	@NamedQuery(name="User.findByLoginName",
> 			query="FROM User WHERE loginName = ?")   // this is useless down here, i just put it here for reference
> })
> public class BaseUser<PK extends Serializable> extends IdEntity<PK> {
> 	private static final long serialVersionUID = -4443522925041212381L;
> 	protected String loginName;
> 	
> 	@Column(unique=true)
> 	public String getLoginName() {
> 		return this.loginName;
> 	}
> 	public void setLoginName(String loginName) {
> 		this.loginName = loginName;
> 	}
> }
> /**
>  * Custom user class for a certain application
>  */
> @Entity
> @Table(name="tbl_user")
> @GenericGenerator(name=IdGen.NAME, strategy=IdGen.AUTO)
> @NamedQueries({
> 	@NamedQuery(name="User.findByLoginName",
> 				query="FROM User WHERE loginName = ?")  // had to copy this up from the base class
> })
> public class User extends BaseUser<Short> {
> 	private static final long serialVersionUID = -6199544722824321999L;
> 	private String firstName;
>   ...
> }
> so in short it would be nice if the scanner would check for that and bind any named queries up to the subclass

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list