简体   繁体   中英

Unable to retrieve JPA Entities in a Wildfly, Arquillian set up

UPDATE Further examination suggest that the problem is somewhere in Wildfly, Hibernate and MySQL. Examining the Wildfly logs I noticed that it does appear to be sending the request to the database:

12:55:04,745 INFO  [stdout] (default task-8) Hibernate: select supplier0_.idsupplier as idsuppli1_0_, supplier0_.name as name2_0_ from supplier supplier0_

I am trying to create a project for my university assignment, but I seem to be unable to get the Entity manager to work properly.

The environment is as follows:

  1. Wildfly 8.20 (running in a remote configuration, although local to eclipse)
  2. MySQL database v5.6.21
  3. eclipse Luna
  4. Arquillian testing framework

The Wildfly server is connected to the MySQL database and when I run the connection test it reports that it is able to connect.

I have created a simple 'Supplier' table with 2 entries added.

The problem is when I run a query to get all the the Supplier Entities it returns an empty list.

Here is my Supplier Entity class

package stockcontrol.entities;

import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the supplier database table.
 * 
 */
@Entity
@Table(name="supplier")
@NamedQuery(name="Supplier.findAll", query="SELECT s FROM Supplier s")
public class Supplier  {
 private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int idsupplier;

@Column(nullable=false, length=255)
private String name;

public Supplier() {
}

public int getIdsupplier() {
    return this.idsupplier;
}

public void setIdsupplier(int idsupplier) {
    this.idsupplier = idsupplier;
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}
}

Here is the Arqillian testClass

package stockcontrol.entities;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class TestSupplier
{
    @Deployment
    public static WebArchive createDeployment()
    {
        return ShrinkWrap.create(WebArchive.class,"mTest.war")
                .addPackages(true, Supplier.class.getPackage())
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE,"beans.xml");
    }

    @PersistenceContext
    EntityManager em;

    @Test
    public void test1()
    {
        Query q = em.createQuery("SELECT s FROM Supplier s",Supplier.class);
        List<Supplier> l = (List<Supplier>)q.getResultList();
        Assert.assertFalse(l.size()==0);
    }
}

And finally my persistence.xml file

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
   xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://xmlns.jcp.org/xml/ns/persistence
        http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
   <persistence-unit name="StockControlDS">
      <jta-data-source>java:jboss/datasources/stockcontrolDS</jta-data-source>
      <properties>
         <!-- Properties for Hibernate -->
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.show_sql" value="false" />
      </properties>
   </persistence-unit>
</persistence>

Any ideas as to why the List is of 0 size in the Test?. It should be of size 2.

All comments and suggestions most welcome.

Finally I found the problem.

The following default hibernate property in the persistence.xml

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

This sets the Hibernate to remove all the tables and data very time the Wildfly server is shutdown. In my Dev environment this happens a lot. I had restarted the wildfly server prior to testing and had removed all my entries from the table.

Size 0 was in fact correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM