[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Re: no persistent classes found for query class
dgeraskov
do-not-reply at jboss.com
Thu May 7 02:56:03 EDT 2009
I created the simplest jpa project. With only 1 entity.
package model;
import javax.persistence.*;
|
| @Entity
| public class Article implements java.io.Serializable {
|
| @Id
| @GeneratedValue(strategy = GenerationType.AUTO)
| private Short id_Article;
|
| @Column(name="ar_name")
| private String arName;
|
| public Article() {}
|
| public Article(short idArticle, String arName) {
| this.id_Article = idArticle;
| this.arName = arName;
| }
| ...get/set
| }
package model;
|
| import java.util.List;
| import javax.persistence.*;
|
| public class R {
|
| /**
| * @param args
| */
| public static void main(String[] args) {
| EntityManagerFactory emf =
| Persistence.createEntityManagerFactory("article_jpa");
|
| // First unit of work
| EntityManager em = emf.createEntityManager();
| EntityTransaction tx = em.getTransaction();
| tx.begin();
| Query max = em.createQuery("from Article a");
| //max.setMaxResults(1);
| List l = max.getResultList();
| for (int i = 0; i < l.size(); i++) {
| Article ar = (Article) l.get(i);
| System.out.println(ar.getIdArticle() + "\t" + ar.getArName());
| }
| if (l.size() == 0){
| for (short i = 1; i < 10; i++){
| Article article = new Article(i, "Article âÃÂÃÂ" + i);
| em.persist(article);
| }
| }
| tx.commit();
| em.close();
|
| emf.close();
| System.out.println("finish");
| }
| }
and persistence.xml
<?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/persistence/persistence_1_0.xsd">
| <persistence-unit name="article_jpa">
| <class>model.Article</class>
| <exclude-unlisted-classes>true</exclude-unlisted-classes>
| <properties>
| <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
| <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
| <property name="hibernate.connection.url" value="jdbc:postgresql:test"/>
| <property name="hibernate.connection.username" value="postgres"/>
| <property name="hibernate.connection.password" value="postgres"/>
|
| <property name="hibernate.current_session_context_class"
| value="org.hibernate.context.JTASessionContext"/>
| </properties>
| </persistence-unit>
| </persistence>
Database table has only 2 columns: id_article and ar_name.
You can use this example as starting point. Change connection settings to your database, create table Article and see how it works.
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4229269#4229269
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4229269
More information about the jboss-user
mailing list