Java Question

Hibernate Error SQLGrammarException (MySQL)

I try to get back a list of elements in an instance Criteria. In the execution I obtain this exception. What is the problem ?

The name of my database is "TransPlusBD".
The name of my table is a "gerant".
But the error indicates me that he(it) does not find the table "TransPlusDB.gerant_gerant", yet this table does not exist.
Normally we have to have his "TransPlusDB.gerant".

Code of the configuration

properties = new Properties();
properties.put("hibernate.dialect","org.hibernate.dialect.MySQLInnoDBDialect");
properties.put("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
properties.put("hibernate.connection.url","jdbc:mysql://(cloud amazone aws).amazonaws.com:3306/TransPlusDB");
properties.put("hibernate.connection.username","xxx");
properties.put("hibernate.connection.password","xxxxxxxxxxxx");
properties.put("hibernate.connection.pool_size","4");
configuration = new Configuration();
configuration.setProperties(properties);

configuration.addAnnotatedClass(Administrator.class);
configuration.addAnnotatedClass(AutoGare.class);
configuration.addAnnotatedClass(Car.class);
configuration.addAnnotatedClass(City.class);
configuration.addAnnotatedClass(Company.class);
configuration.addAnnotatedClass(transplus.models.Configuration.class);
configuration.addAnnotatedClass(DateDeparture.class);
configuration.addAnnotatedClass(Departure.class);
configuration.addAnnotatedClass(HoursDeparture.class);
configuration.addAnnotatedClass(Luggage.class);
configuration.addAnnotatedClass(Manager.class);
configuration.addAnnotatedClass(Passenger.class);
configuration.addAnnotatedClass(PlanningVoyage.class);
configuration.addAnnotatedClass(Route.class);
configuration.addAnnotatedClass(Stopover.class);
configuration.addAnnotatedClass(SysAdmin.class);
configuration.addAnnotatedClass(Ticket.class);
configuration.addAnnotatedClass(TypeCar.class);

configuration.addAnnotatedClass(ModificationLuggage.class);
configuration.addAnnotatedClass(ModificationTicket.class);
configuration.addAnnotatedClass(PassageRoute.class);

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();


Code of my class

@Entity
@Table(name = "gerant")
public class Manager implements Serializable // Table Gerant
{
@Id
@GeneratedValue
@Column(name = "code_gerant")
private long code_manager;

@Column(name = "matricule_gérant",unique = true)
private String matricule_manager;
@Column(name = "nom_gerant")
private String lastName_manager;
@Column(name = "prenom_gerant")
private String firstName_manager;
@Column(name = "password_gerant",nullable = false)
private String password_manager;
@Column(name = "login_gerant",unique = true,nullable = false)
private String login_manager;
@Column(name = "poste_gerant")
private String function_manager;
@Column(name = "actif_gerant")
private boolean enabled_manager;

@Enumerated(EnumType.ORDINAL)
@Column(name = "privilege_gerant")
private Privilege privilege;

@ManyToOne
@JoinColumn(name = "code_manager", foreignKey = @ForeignKey(name = "fk_gerant_manager"))
private Administrator administrator;

@ManyToOne
@JoinColumn(name = "over_gerant", foreignKey = @ForeignKey(name = "fk_over_gerant"))
private Manager overManager;

@Expose // Annotation for Gson
@OneToMany(cascade = CascadeType.ALL,orphanRemoval = false)
private List<Manager> underManagers = new ArrayList<>();


Code for the recovery of the list.

public String getAllManager()
{

if(session.isOpen())
{
Transaction transaction = null;
try
{
transaction = session.beginTransaction();
transaction.begin();
Criteria criteria = session.createCriteria(Manager.class);
List list = criteria.list();
transaction.commit();
if(list != null)
{
if(!list.isEmpty())
return serializeTab(list);
}
return null;
}
catch (Exception e)
{
if(transaction != null)
transaction.rollback();
e.printStackTrace();
}
}
return null;
}


Here is the raised exception

this = {ServiceManager@3681}
transaction = {TransactionImpl@3683}
transactionCoordinator = {JdbcResourceLocalTransactionCoordinatorImpl@3979}
transactionDriverControl = {JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl@3980}
valid = false
e = {SQLGrammarException@3954} "org.hibernate.exception.SQLGrammarException: could not extract ResultSet"
sqlException = {MySQLSyntaxErrorException@3958} "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'TransPlusDB.gerant_gerant' doesn't exist"
SQLState = "42S02"
vendorCode = 1146
next = null
detailMessage = "Table 'TransPlusDB.gerant_gerant' doesn't exist"
cause = {MySQLSyntaxErrorException@3958} "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'TransPlusDB.gerant_gerant' doesn't exist"
stackTrace = {StackTraceElement[0]@3961}
suppressedExceptions = {Collections$UnmodifiableRandomAccessList@3962} size = 0
sql = "n/a"


Help I PLEASE

Answer

The table gerant_gerant is a join table for a self association for this

@OneToMany(cascade = CascadeType.ALL,orphanRemoval = false)
private List<Manager> underManagers = new ArrayList<>();

You need to let Hibernate to create this table using the hibernate.hbm2ddl.auto property or you can create it manually with constraints ( a foreign key, a unique key).

You can specify a join table name with the @JoinTable annotation.