MeknessiHamida MeknessiHamida - 4 months ago 18
SQL Question

EJB and Hibernate: Table dosen't exist

I am working on this application where I need to get a result list from MySql database, but everytime I run it on the server(wildfly 9) I am getting an error that the table dosen't exist.Here is the code in my EJB :

@Stateless
@LocalBean
public class MessageService {

@PersistenceContext(unitName="primary")
EntityManager em;

@SuppressWarnings("unused")
public List<Message> customers = new ArrayList<Message>();

public List<Message> findAll() {
customers= em.createQuery("select c from Message c").getResultList();
//customers = query.getResultList();
System.out.println(customers.size());
return customers;
}


and this is My Entity :

@Entity
@NamedQuery(name="Message.findAll", query="SELECT m FROM Message m")
public class Message implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private int idMessage;

private String discriminateur;

private int numFenetre;

private String texte;


and this is my persistence.xml :

<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="primary">
<!-- If you are running in a production environment, add a managed
data source, this example data source is just for development and testing! -->
<!-- The datasource is deployed as <EAR>/META-INF/MavenEar-ds.xml, you
can find it in the source at ear/src/main/application/META-INF/MavenEar-ds.xml -->
<jta-data-source>java:/mysql</jta-data-source>
<class>com.model.entities.Message</class>
<properties>
<!-- Properties for Hibernate -->
<!-- <property name="hibernate.hbm2ddl.auto" value="create-drop" />-->
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>
</persistence>


PS: I am did set a datasource on wildly that links to the database.

Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:91)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2066)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1863)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839)
at org.hibernate.loader.Loader.doQuery(Loader.java:910)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
at org.hibernate.loader.Loader.doList(Loader.java:2554)
at org.hibernate.loader.Loader.doList(Loader.java:2540)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
at org.hibernate.loader.Loader.list(Loader.java:2365)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:497)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:236)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1300)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
... 126 more
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'christina.message' doesn't exist
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)
at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:504)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
... 142 more

Answer

Try adding the following to your persistence.xml

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

I think it might be looking for christina.christina.message. otherwise the error would just say table "message" not found. how is your entity annotated? Are you using a custom naming strategy or dialect?