File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes named queries can be defined only on an entity - problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "named queries can be defined only on an entity - problem" Watch "named queries can be defined only on an entity - problem" New topic
Author

named queries can be defined only on an entity - problem

olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
hi there,

i have a new problem.
i have a domain object called "Lecture", and in the database its called "Lectures"
i want to use hibernate to manage this database stuff so i created the annotations:


the interpreter says:
named queries can be defined only on an entity or a mappedsuperclass class.
i have no idea whats the problem because with @entity i tell hibernate that its a entity, dont i?

ps.: and yes, i used the netbeans "do the magic"-create objects from database sheme ;)
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

I think that in this case you need to refer to Lecture in your named queries as Lecture because that is the name of the class. You never need to refer to the name of the table anywhere in your code (HQL included) with the exception of the @Table annotation or if you ever do plain sql queries (that is without hibernate).

Try changing your named queries to use the name of the class instead of the name of the table and see if that works.

Cheers,
Rachel
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
Hi Rachel,

thats what i tried first ;)
And no, it doesnt resolve the problem.
What's funny, everything works fine... as far as i can say (because the portlet is not finished yet).

Btw. i changed the database fields (from plural to singular) - imo it looks ugly now, but its working...

Any other ideas what could be the problem?
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hmm, I'm interested to find out why this is. I'm busy putting an example together so that I can see what Hibernate does.

At what point do you get the error?
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
Netbeans only shows the error message.
If you'd like to i give you all the code + database layout
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Thanks for the offer but I think I'll try further with a simple setup and see what I can find. I've put it together in IntelliJ and it's not showing any error messages so I'm going to try reading and writing some data with the queries and see how far I get.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
I set it up to store, retrieve and delete some lectures using the named query annotation that you had pasted above.

When running the code as is I got these errors

Then I changed one of the queries to use Lecture instead of Lectures and got an error on the other named query

and when I changed them both to be Lecture instead of Lectures there was no error.

So in the end the head of Lecture looked like this


Are you able to run your code at all or does the error from Netbeans prevent you from compiling and running it?




olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
no i can compile and run the portlet fine (no it doesnt run fine, but thats because im absolutly new to hibernate, spring and struts)
the output of the sql querys looks also ok, like:



if you need further informations, just reply
maybe its just a netbeans bug?
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
If it's generating the right sql for you then I think I'd be looking at if it's a problem with netbeans. I did a quick search and found this at Stack Overflow.
 
Don't get me started about those stupid light bulbs.
 
subject: named queries can be defined only on an entity - problem