6

I'm using Hibernate and a MySql server. I use multiple databases as "namespaces" (e.g. users, transactions, logging etc.).

So, I configued Hibernate to NOT connect to a particular database :

url = jdbc:mysql://127.0.0.1/

The databases where tables are located are defined in the hbm files through the catalog attribute :

<class name="com.myApp.entities.User" table="user" schema="" catalog="users"> ...

When I want to load some data, everything works fine and Hibernate seems to generate the expected SQL queries (by using the catalog prefix in the table names) e.g. :

select id from users.user

However, when I try to add a new record, Hibernate don't use the from [catalog].[table_name] syntax anymore. So I get a MySQL error 'No database selected'.

select max(id) from user

Hibernate is trying the get the future id to create a new record, but it doesn't specify in which database is located the table, it should be :

select max(id) from users.user

Why is Hibernate generating this invalid query ? Have someone ever experienced this problem ?

10
  • What's the version of the hibernate you're using? Commented Jun 29, 2013 at 12:59
  • I'm using Hibernate-4.2.2 Commented Jun 29, 2013 at 13:04
  • what happens if you use: schema="users" catalog="users" ? Commented Jun 29, 2013 at 13:14
  • 1
    Did you set the property hibernate.dialect to org.hibernate.dialect.MySQLDialect? Maybe hibernate fails to determine this automatically... Commented Jun 29, 2013 at 13:44
  • 3
    Have you seen stackoverflow.com/questions/1566263/… ? Commented Jul 1, 2013 at 13:00

1 Answer 1

1
+100

You need to specify the schema for the generator. See this question on SO for a more detailed answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.