Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [EJB3 - Hibernate] TABLE is not mapped

    Ciao a tutti,
    mi sto sporcando le mani con EJB3, Annotations e Hibernate.

    Il mio problema è per il momento legato ad un errore proveniente da Hibernate:

    Hibernate version:3.2.2.ga and JBoss 4.1 - NetBeans 5.5 - MySql 5

    stack trace dal client
    codice:
    11:36:14,666DEBUG SecurityAssociation:143 - Using ThreadLocal: false
    11:36:14,715DEBUG Client:514 - invoke called, but our invoker is disconnected, discarding and fetching another fresh invoker for: InvokerLocator [socket://127.0.0.2:3873/]
    11:36:14,715DEBUG SocketClientInvoker:275 - connect called for: org.jboss.remoting.transport.socket.SocketClientInvoker@15d56d5
    javax.ejb.EJBException: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: USR is not mapped [SELECT DISTINCT OBJECT(k) FROM USR k WHERE k.username = ?1]
    ...
    
    Caused by: org.hibernate.hql.ast.QuerySyntaxException: USR is not mapped [SELECT DISTINCT OBJECT(k) FROM USR k WHERE k.username = ?1]
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
    EJB3 method
    codice:
        public Usr getUsrByUsername(String _username) throws UsernameException {
                if(_username==null) throw new UsernameException("The username value cannot be null.");
                Usr user = (Usr)em.createQuery("SELECT DISTINCT OBJECT(k) " +
                        "FROM USR k WHERE k.username = ?1").getSingleResult();
                return user;
            }

    Persistence.xml

    codice:
        <?xml version="1.0" encoding="UTF-8"?>
        <persistence version="1.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/persisten...stence_1_0.xsd">
            <persistence-unit name="Users-EJBModulePU" transaction-type="RESOURCE_LOCAL">
                <!-- Jboss uses hibernate as default persistence engine
                <provider>org.hibernate.ejb.HibernatePersistence</provider> -->
                <jta-data-source>java:/MySqlDS</jta-data-source>
                <exclude-unlisted-classes>false</exclude-unlisted-classes>
                <properties>
                    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
                    
                    <property name="hibernate.show_sql" value="true"/>
                    <property name="hibernate.format_sql" value="true"/>
                    
                    <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                </properties>
            </persistence-unit>
        </persistence>


    annotations - PoJO
    codice:
        @Entity
        @Table(name="USR")
        public class Usr implements java.io.Serializable {
    Qualche idea sul perchè ho quell'errore?
    Grazie in anticipo
    Ciao

  2. #2
    Utente di HTML.it L'avatar di nether
    Registrato dal
    Dec 2006
    Messaggi
    376
    nelle query con hibernate solitamente non si usa SQL puro (anche se resta la possibilita', ma non e' il tuo caso e solitamente non ci sono molti motivi per farlo) ma HQL (che e' una specie di SQL "adattato" all'uso con gli oggetti anziche' le tabelle).

    tu hai scritto questa query:
    codice:
    ("SELECT DISTINCT OBJECT(k) " +
                        "FROM USR k WHERE k.username = ?1")
    l'oggetto "em" di cui fai uso credo sia un'istanza dell'EntityManager delle JPA, che personalmente non ho mai usato, per cui non so come si settino i parametri al suo interno (ho visto che ne hai almeno uno da settare: lo username).
    Ti dico come lo farei io con hibernate usando le api "tradizionali", non dovrebbe volerci molto ad adattarlo...
    codice:
    Session s = .... ; //ti fai dare una Session dalla tua SessionFactory
    Query q = s.createQuery("from Usr k where k.username = :nomeutente); //"select *" puo' essere omesso
    q.setString("nomeutente", "pippo");
    Usr user = (Usr) q.uniqueResult();
    Devi quindi usare i nomi delle tue entita' (nel caso "Usr") e non il nome delle tabelle in cui queste entita' sono mappate (avevi scritto "USR" nella tua query, e hibernate non trovava un mapping corrispondente all'entita' "USR", perche' in effetti non esiste)

    p.s. non capisco il senso di fare select distinct anziche' select normale...

  3. #3
    try {
    return (Usr)em.createQuery("from Usr u where u.username = :_username")
    .setParameter("_username",_username).getSingleResu lt();

    } catch(NoResultException ex) {
    //Usr not found
    return null;
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.