-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Getting the id from an IdentifierGenerator
PostPosted: Sat Mar 06, 2010 10:57 pm 
Newbie

Joined: Sat Mar 06, 2010 10:38 pm
Posts: 3
Hi All,

I'm having this wee problem with accessing a stored procedure via an IdentifierGenerator implemented class, to obviously get the unique id of the table that I'm currently dealing with. I'm using annotations which seems to complicate things further (mainly due to the fact that I'm fairly new to hibernate I'd imagine). I realise that it's an issue with the named query not being found, but I'm not sure how to rectify it. Anyways, following is my class, and following that is the stacktrace of the problem - any help would be greatly received.

Code:
@NamedNativeQuery(name="getNextId",
      query="? = call usp_getNextId (:tableName)",
      resultClass = Integer.class)
public class IdGenerator implements IdentifierGenerator, Configurable {

    private String tableName;

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public void configure(Type type, Properties properties, Dialect dialect) throws MappingException {
        setTableName(properties.getProperty(PersistentIdentifierGenerator.TABLE));
    }

    public Serializable generate(SessionImplementor session, Object obj)
            throws HibernateException {
        return getNextNumber(session);
    }

    private int getNextNumber(SessionImplementor session) {
        String storedProcedure = "getNextId";
        Integer nextValue = 0;
        try {
            PreparedStatement statement =
                    session.getBatcher().prepareSelectStatement(storedProcedure);
            statement.setString(1, tableName);
            try {
                ResultSet resultSet = statement.executeQuery();
                try {
                    while (resultSet.next()) {
                        nextValue = Integer.parseInt(resultSet.getString(1));
                    }
                } finally {
                    resultSet.close();
                }
            } finally {
                session.getBatcher().closeStatement(statement);
            }
        } catch (SQLException e) {
            throw JDBCExceptionHelper.convert(session.getFactory()
                    .getSQLExceptionConverter(), e,
                    "could not fetch initial value for increment generator",
                    storedProcedure);
        }
        return nextValue;
    }
}


---------------------------

org.hibernate.MappingException: Named query not known: getNextId
at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:93)
at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1288)
at com.wraith.money.model.db.schema.IdGenerator.getNextNumber(IdGenerator.java:53)
at com.wraith.money.model.db.schema.IdGenerator.generate(IdGenerator.java:43)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:523)
at org.springframework.orm.hibernate3.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:740)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:737)
at com.wraith.money.model.db.metadata.dao.GenericDAOHibernateImpl.saveUpdate(GenericDAOHibernateImpl.java:109)
at com.wraith.money.model.db.metadata.Metadata.saveCustomTable(Metadata.java:84)
at com.wraith.money.model.db.schema.CustomTableEntityTest.testInsertCustomTables(CustomTableEntityTest.java:44)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at com.intellij.rt.junit4.Junit4ClassSuite.run(Junit4ClassSuite.java:99)
at junit.textui.TestRunner.doRun(TestRunner.java:116)
at com.intellij.rt.execution.junit.IdeaTestRunner.doRun(IdeaTestRunner.java:94)
at junit.textui.TestRunner.doRun(TestRunner.java:109)
at com.intellij.rt.execution.junit.IdeaTestRunner.startRunnerWithArgs(IdeaTestRunner.java:22)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:118)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
Hibernate: getNextId

Many Thanks,
R.


Top
 Profile  
 
 Post subject: Re: Getting the id from an IdentifierGenerator
PostPosted: Mon Mar 08, 2010 3:25 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I don't think Hibernate looks for named queries in other classes than entity classes. Try moving the query definition to a different class. There is also a possibility to define it in an XML file. http://docs.jboss.org/hibernate/stable/ ... -query-hql


Top
 Profile  
 
 Post subject: Re: Getting the id from an IdentifierGenerator
PostPosted: Mon Mar 08, 2010 12:50 pm 
Newbie

Joined: Sat Mar 06, 2010 10:38 pm
Posts: 3
Hi Nordborg,

Thanks for getting back to me. I assumed as much, and I tried defining it in my @MappedSuperclass - from which all my @Entity classes inherit, but with the same result, my preference would be to define it just once - would I be correct in assuming that I'd have to define this @NamedNativeQuery in every @Entity class? Since it is used in every one of them? Is there no facility for it to be defined just once and then used?

I toyed with the idea of defining everything via an XML file, but thought better of it since my classes would rarely change, and I wanted to get used to using annotations.

Thanks,
R.


Top
 Profile  
 
 Post subject: Re: Getting the id from an IdentifierGenerator
PostPosted: Mon Mar 08, 2010 1:57 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I have no actual experience with neither annotations nor with named queries but the documentation says:

Quote:
@NamedQuery and @NamedQueries can be defined at the class level or in a JPA XML file. However their definitions are global to the session factory/entity manager factory scope. A named query is defined by its name and the actual query string.


I interpret this as you can define a named query anywhere you like and then use it from everywhere. So, pick one of your entity classes and put your named queries in there if you don't want to use the xml file. I hope it works....


Top
 Profile  
 
 Post subject: Re: Getting the id from an IdentifierGenerator
PostPosted: Tue Mar 09, 2010 5:41 pm 
Newbie

Joined: Sat Mar 06, 2010 10:38 pm
Posts: 3
Good interpretation, I'd agree. Goes to show...RTFM ;-) Thanks Nordborg.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.