Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6591237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:23:58+00:00 2026-05-25T17:23:58+00:00

Assume the following trivial class: package Sample; public class Status { private Long id;

  • 0

Assume the following trivial class:

package Sample;  

    public class Status {

        private Long id;
        private String status;

        public Status() {}

        public Status(String status) {
        this.status = status;
        }   

        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
    }  

For this class I have defined the hibernate-mapping in the Status.hbm.xml.
This file, describes to Hibernate how to map the class to a table.
In the hibernate.cfg I have defined how to connect to my Database (MySQL).

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.username">root</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>

        <mapping resource="sample/Status.hbm.xml"/>
    </session-factory>
</hibernate-configuration>  

What I do not undertand is the following:
With this configuration when I try to use the org.hibernate.Session to save an object of Status to the database, it complaints that there is no database to connect.

842 [main] ERROR org.hibernate.util.JDBCExceptionReporter – No
database selected

If I add an existing database as part of the URL connection string of hibernate.cfg.xml i.e.

<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>  

I get a different error complaining that the table test.status does not exist.
I was expecting that Hibernate would create it.
The code is succesfull only if I do:

SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.create(false, true);  

So my question is, what is the proper process to use Hibernate and create a schema and a database and its tables starting from Java.
I do not want to manually create a database in MySQL for example from an MySQL client.
Is the above approach with the SchemaExport the correct way?
What is the best option for the start from Java DB design?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:23:58+00:00Added an answer on May 25, 2026 at 5:23 pm

    Having Hibernate create the table structure is fine for testing, but once the application has been deployed to production and is actually running, you will NOT want hibernate to create the database every time you re-deploy because it will wipe out your data. Getting used to creating ALTER scripts for managing database changes is a good habit to get into, since most development is not new but is updating an existing system.

    That being said, yes, the way you have indicated is the correct way to have Hibernate create the database schema. You can also set that to happen in the Hibernate config file, but I don’t remember the property off the top of my head.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets assume following classes definition: public class A { public final static String SOME_VALUE;
Assume the following class: public class MyEnum: IEnumerator { private List<SomeObject> _myList = new
Assume following: public class MyFunkyTable : DbObject { // this class will be generated
Assume the following scenario (with relational databases), class Currency { long id; String code;
Let's assume following code snippet: public class NotThatWellWrittenClass { public static void doSmth() {
Assume the following type definitions: public interface IFoo<T> : IBar<T> {} public class Foo<T>
assume this following function: int binaryTree::findHeight(node *n) { if (n == NULL) { return
Assume the following: we have class B, which is a private class nested inside
In C++, assume following class hierarchy: class BaseClass { }; class ChildClass : public
Assume the following mapping entries: < class name=Customer table=customer> <id name=InternalId column=uintCustomerId type=long> <generator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.