• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar

Thoughts on Java

  • Tutorials ▼
    • Hibernate & JPA Tutorials
    • Hibernate Tips
    • Tutorials on YouTube
  • Books & Training ▼
    • Hibernate Tips Book
    • Online Training
    • On-Site Training
  • Consulting ▼
    • Consulting Call
    • Project Coaching
  • About ▼
    • About Thoughts on Java
    • Support Thoughts on Java
    • Resources
  • Member Library
You are here: Home / Hibernate / Hibernate Tips / Hibernate Tips: How to define schema and table names
Special thanks to all Thoughts on Java Supporters for supporting this article!

Hibernate Tips: How to define schema and table names

By Thorben Janssen 12 Comments

  • tweet 
  • share 
  • share 
  • share 
  • share 
  • share 
  • e-mail 

Hibernate Tips _ How to define schema and table names
Hibernate Tips is a series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question you like me to answer, please leave a comment below.

Question:

How can I define the name of the database schema and table which will be used to persist my entity?

Don’t want to read? You can watch it here!

Follow me on YouTube to not miss any new videos.

Solution:

You can define the schema and table name with the schema and name attributes of the javax.persistence.Table annotation. You can see an example of it in the following code snippet. You just have to add the @Table annotation to your entity class and set the name and schema attributes.

When you now use the entity, Hibernate uses the provided schema and table names to create the SQL statements.
The following code snippet persists a new Author entity and performs a query to get all Author entities with the given first name.

As you can see in the following log output, Hibernate persists the Author entity to the author table in the bookstore database schema and performs the SELECT statement on the same table.

Get this Hibernate Tip as a printable PDF!

Join the free Thoughts on Java Library to get access to lots of member-only content, like a printable PDF for this post, lots of cheat sheets and 2 ebooks about Hibernate.
Join Now!

Already a member? Login here.

Learn more

If you want to dive deeper into Hibernate’s mapping annotations and other advanced features, you should have a look at my Advanced Hibernate Online Training. I just opened the registration and the early bird discount is available until October 24th.
 

Hibernate Tips Book


Get more recipes like this one in my new book Hibernate Tips: More than 70 solutions to common Hibernate problems.

It gives you more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching and statically and dynamically defined queries.

Get it now as a paperback, ebook or PDF.

  • tweet 
  • share 
  • share 
  • share 
  • share 
  • share 
  • e-mail 

Related posts:

  1. Hibernate Tips: How to create a database setup script based on entity mappings
  2. Hibernate Tips: How to map a bidirectional many-to-one association
  3. Hibernate Tips: How to Increase the Version of the Parent Entity When Updating a Child Entity
  4. Hibernate Tips: Easiest way to manage bi-directional associations
Become a Thoughts on Java Supporter to claim your member perks and to help me write more articles like this.

Filed Under: Hibernate Tips, JPA Tagged With: Configuration, Mapping

Implement Your Persistence Layer with Ease

Learn More About Hibernate

Need Some Help with Your Project?

Reader Interactions

Comments

  1. Coder says

    January 11, 2017 at 2:17 pm

    Thanks for sharing the tip. Kindly let me know how the hbm.xml file will look for this case. Generally we provide the table name, schema and table column details in the hbm.xml file. Since now as per your tip we have now mentioned the schema name in the java persistence file do we need to provide the schema name in the hbm.xml file?

    Reply
    • Thorben Janssen says

      July 27, 2018 at 7:20 am

      Hi,
      you can use a schema element in your mapping configuration as described here: https://thoughts-on-java.org/mapping-definitions-jpa-hibernate-annotations-xml/#customizedEntityMapping
      Regards,
      Thorben

      Reply
  2. Cheenu says

    August 8, 2017 at 1:09 pm

    This works for hsql, but does not seem to work for mysql. Any thoughts.

    Reply
    • Thorben Janssen says

      August 8, 2017 at 1:53 pm

      Hi,

      I haven’t tried this on a MySQL but it’s probably because MySQL doesn’t provide real schema support. For MySQL, a schema is just a synonym for a database.

      Regards,
      Thorben

      Reply
      • Tim says

        November 15, 2017 at 5:55 pm

        So what is the correct way to have this functionality if using MySQL?

        Reply
        • Tim says

          November 16, 2017 at 7:17 pm

          If using MySQL, the correct way is to use @Table(name=”schema.table_name”), but you also have to make sure that you do not define a default database/schema in your data source url configuration. See https://stackoverflow.com/questions/47296766/spring-boot-jpa-does-not-prepend-schema-name-to-tables-in-query/47336516#47336516.

          Reply
  3. sohal amin says

    October 14, 2017 at 4:10 pm

    Hibernate configuration for the multi schema and spring

    I need to configure Hibernate with mysql and want to create two schema with relation. I used hibernate 5.2 version.

    For ex.

    Schema1 — Table1 — Table2

    Schema2 — Table1 — Table2

    Schema1.table has relation with schema2.table1.

    How can I achieve this with hibernate and what kind of configuration I need to create. Which kind of notation I need to use with pojo.

    Thanks

    Reply
    • Thorben Janssen says

      July 27, 2018 at 7:16 am

      You can specify the schema for each entity as described in this article and then define a normal relationship between your entities.

      Regards,
      Thorben

      Reply
  4. jgabrielehay says

    December 11, 2017 at 9:45 pm

    Took me a while to realise that this blog replaced the quote pairs. As displayed (copy/pasted), the @Table annotations do not compile.

    Reply
    • Thorben Janssen says

      December 29, 2017 at 5:06 pm

      Fixed, thanks!

      Reply
  5. Sanket says

    July 26, 2018 at 9:25 pm

    What if schema name is dynamic ?

    Reply
    • Thorben Janssen says

      July 27, 2018 at 7:14 am

      Are all your tables located in the same schema?
      If that’s the case and you can adapt the configuration of your persistence layer, you could set the default schema in your persistence.xml with this property . A probably better option is to bootstrap Hibernate programmatically and set the default schema during that process.

      Regards,
      Thorben

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Join over 10.000 developers
in the
Thoughts on Java Library

Ebooks Sidebar Get free access to ebooks, cheat sheets and training videos.
Join Now!
Don't like ads?
Become a Thoughts on Java Supporter.
Special Launch Price

Let’s Connect


Thorben Janssen
Independent consultant, trainer and author
  • Facebook
  • GitHub
  • Google+
  • LinkedIn
  • Twitter
  • xing
  • YouTube

Speaking at

7th February 2019
JUG Ostfalen (Germany):
Hibernate Tips 'n' Tricks: Typische Probleme schnell gelöst (Talk - German)

19th March 2019
JavaLand 2019 (Germany):
Hibernate Tips 'n' Tricks: Typische Probleme schnell gelöst (Talk - German)

21st March 2019
JavaLand 2019 (Germany):
Hibernate + jOOQ + Flyway = Die besten Frameworks in einem Stack (1-day Workshop - German)

6th-10th May 2019
JAX 2019 (Germany):
Hibernate Workshop: Komplexe Lösungen jenseits von CRUD (2-day Workshop - German)

6th-10th May 2019
JAX 2019 (Germany):
Spring Data JDBC vs. Spring Data JPA – Wer macht das Rennen? (Talk - German)

6th-10th May 2019
JAX 2019 (Germany):
Microservices mit Hibernate – typische Probleme und Lösungen (Talk - German)

Looking for an on-site training?

Featured Post

14 YouTube Channels You Should Follow in 2019

Getting Started With Hibernate

Entities or DTOs – When should you use which projection?

Ultimate Guide – Association Mappings with JPA and Hibernate

Ultimate Guide to JPQL Queries with JPA and Hibernate

Recent Posts

  • Hibernate Tips: How to Customize a Constructor Expression for Different Subclasses
  • Hibernate Tips: How to Map java.time.Year with JPA and Hibernate
  • Why, When and How to Use DTO Projections with JPA and Hibernate
  • Hibernate Tip: Map a bidirectional one-to-one association with shared composite primary key
  • Implementing the Repository pattern with JPA and Hibernate
  • Hibernate Tips: How to Handle NULL Values while Ordering Query Results in a CriteriaQuery
  • 6 Hibernate Best Practices for Readable and Maintainable Code
  • Can you use Hibernate/EclipseLink/JPA for your microservice?
  • 14 YouTube Channels You Should Follow in 2019
  • Hibernate Tips: How To Map an Entity to a Query
Don't like ads?
Become a Thoughts on Java Supporter.

Copyright © 2019 Thoughts on Java

  • Impressum
  • Disclaimer
  • Privacy Policy