代码之家  ›  专栏  ›  技术社区  ›  MeknessiHamida

EJB和Hibernate:表不存在

  •  0
  • MeknessiHamida  · 技术社区  · 10 年前

    我正在处理这个应用程序,需要从MySql数据库中获取结果列表,但每次我在服务器上运行它(wildfly 9)时,我都会收到一个错误,即表不存在。以下是我的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;
       }
    

    @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;
    

    这就是我的坚持。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:我确实设置了一个连接到数据库的数据源。

    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
    
    1 回复  |  直到 10 年前
        1
  •  1
  •   Tea Curran    10 年前

    尝试将以下内容添加到persistence.xml

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

    我想可能是在找克里斯蒂娜。克里斯蒂娜的留言。否则,错误只会显示表“message”not found。您的实体是如何注释的?您是否使用自定义命名策略或方言?