Eclipse Hibernate.cfg.xml is linking "sys" database from MySQL

Viewed 283

When I use Hibernate to connect to a MySQL database, bookstoredb, in Eclipse, a connection to the MySQL sys database also shows up!

enter image description here

I'm using the following:

  • Ubuntu 18.04.5 LTS (desktop)
  • Eclipse 2020-6 (4.16.0) w/JBoss Tools, Hibernate-Tools plugin.
  • MySQL Ver 8.0.21 for Linux on x86_64 (MySQL Community Server - GPL)

Note: this is a local desktop project, I am working through a tutorial. I will NOT be using root/admin with MySQL down the road.

Following is the complete hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">admin</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookstoredb?serverTimezone=UTC</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    </session-factory>
</hibernate-configuration>

Am I misunderstanding, shouldn't the connection.url, as given, ONLY connect to bookstoredb? Why is "sys" connecting also?

This becomes even more of a problem when using the tool to reverse-engineer the database tables into java classes. I'm getting not only the bookstoredb tables as java classes but every table in the sys database as well. And there are a lot of them, as I am sure folks well know.

How do I unlink the sys database? Or set things up so I don't link to it in the first place?

2 Answers

I guess sys is the default schema in your MySQL workbench.

Go to your Mysql workbench and drop the schema/because it is importing all the schemas from your local server.

or

Custom :- Open your workspace(eclipse("run" tab on top of screen->hibernate code genration) -> Go to hibernate code generation( -> click main-> revenge.xml->select new -> select folder-> configure table filter-> (1)select database you want to select-> import all table-> apply -> Run .

Now you can see all the tables in the location you have selected. If you want to customer export you could also do so after (1) step above

For those with a similar issue to this. Hibernate tools reverse engineering reads all schemas. Therefor specifying the database doesn't work. So you need a custom schema selection tag to specify which database you want reverse engineer from. This is done by creating a hibernate.reveng.xml file and adding something like this where demo is the name of the database

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering SYSTEM "http://hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd">

<hibernate-reverse-engineering>
    <schema-selection match-catalog="demo" match-schema=".*" match-table=".*"/>
</hibernate-reverse-engineering>

Related