emmanuel wrote:
I dunno, there is a unit test for that. You will have to describe things further.
thank's in advance
i work with maven and eclipse 3.3 europe
testValidatorInsert() is ok, throw this error
//-----------------------------------
javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: org.hormiga.domain.Cliente.nombre
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:630)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:219)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at
//----------------------------------
testValidatorUpdate() is my problem con't throw any error
my context is:
persistence.xml
//--------------------------
<persistence 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/ ... ce_1_0.xsd"
version="1.0">
<persistence-unit name="pu-jpizza" transaction-type="RESOURCE_LOCAL">
<class>org.hormiga.domain.Cliente</class>
<class>org.hormiga.domain.Articulo</class>
</persistence-unit>
</persistence>
//-------------------------
aplicationCOntext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/b ... ns-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/t ... tx-2.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/jdbc.properties"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- jpaVendorAdapter Hibernate, injected into emf -->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="HSQL"/>
<property name="showSql" value="false"/>
<!-- Data Definition Language script is generated and executed for each run -->
<property name="generateDdl" value="true"/>
<!-- <property name="hibernate.dialect" value="${hibernate.dialect}"/> -->
<!-- <property name="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}"/> -->
<!-- Print SQL to stdout -->
<!-- <property name="hibernate.show_sql" value="${hibernate.show_sql}" /> -->
<!-- if you want to use Hibernate's auto schema generation, uncomment the following line -->
<!-- property name="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}" /-->
</bean>
</property>
<!--<property name="hibernateProperties"> -->
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!-- <prop key="hibernate.show_sql">false</prop> -->
<prop key=" hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
<!-- loadTimeWeaver -->
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>
</property>
<!-- <property name="eventListeners">
<map>
<entry key="pre-update">
<bean class="org.hibernate.validator.event.JPAValidateListener"/>
</entry>
<entry key="pre-insert">
<bean class="org.hibernate.validator.event.JPAValidateListener"/>
</entry>
</map>
</property> -->
</bean>
<!-- <tx:annotation-driven /> -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="clienteDAO" class="org.hormiga.dao.ClienteDAO" />
<bean id="articuloDAO" class="org.hormiga.dao.ArticuloDAO" />
</beans>
//---------------------------------------------------------------------
Cliente.java
package org.hormiga.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
@Entity
//@EntityListeners(org.hibernate.validator.ValidatorClass.class)
public class Cliente {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NotNull
// @Column(updatable = true, name = "nombre", nullable = false)
private String nombre;
@Length(min = 2)
// @Min(2)
private String telefono;// = "";
private String direccion;// = "";
private String cp;// = "";
public void setNombre(String nombre) {
this.nombre = nombre;
}
public void setTelefono(String telefono) {
this.telefono = telefono;
}
public void setDireccion(String direccion) {
this.direccion = direccion;
}
public void setCp(String cp) {
this.cp = cp;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public String getTelefono() {
return telefono;
}
public String getDireccion() {
return direccion;
}
public String getCp() {
return cp;
}
}
//--------------------------------------------------------------
clieneDAO.java
package org.hormiga.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.hormiga.domain.Cliente;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class ClienteDAO implements IClienteDAO {
// extends HibernateDaoSupport
@PersistenceContext(unitName = "pu-jpizza")
private EntityManager entityManager;
public Cliente findById(long id) {
return entityManager.find(Cliente.class, id);
}
@SuppressWarnings("unchecked")
public List<Cliente> findAll() {
return entityManager.createQuery("from Cliente").getResultList();
}
public void delete(Cliente c) {
/*
* Cliente c = find(id); if (person != null) { em.remove(person); }
*/
entityManager.remove(c);
}
public Cliente insert(Cliente c) {
entityManager.persist(c);
return c;
}
public Cliente update(Cliente c) {
return entityManager.merge(c);
}
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@SuppressWarnings("unchecked")
public List<Cliente> findByNombre(String par) {
Query q = entityManager
.createQuery("Select c from Cliente c where LOWER(c.nombre) like LOWER(:param)");
return q.setParameter("param", par).getResultList();
}
}
//-------------------------------------------------------------------
ClienteDAOIntegrationTest.java
package org.hormiga;
import java.util.List;
import org.hormiga.dao.IClienteDAO;
import org.hormiga.domain.Cliente;
import org.springframework.test.jpa.AbstractJpaTests;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class ClienteDAOIntegrationTest extends AbstractJpaTests {
private IClienteDAO clienteDAO;
private long testId;
public void setClienteDAO(IClienteDAO clienteDAO) {
this.clienteDAO = clienteDAO;
}
public ClienteDAOIntegrationTest() {
super();
setDependencyCheck(false);
setAutowireMode(AUTOWIRE_BY_NAME);
}
@Override
protected String[] getConfigLocations() {
return new String[] { "classpath:/applicationContext.xml" };
}
@Override
@BeforeClass
protected final void onSetUpInTransaction() throws Exception {
Cliente c1 = new Cliente();
c1.setNombre("Paddy");
c1.setTelefono("555");
Cliente c2 = new Cliente();
c2.setNombre("Claire");
c2.setTelefono("555");
Cliente c3 = new Cliente();
c3.setNombre("Harry");
c3.setTelefono("555");
clienteDAO.insert(c1);
clienteDAO.insert(c2);
clienteDAO.insert(c3);
testId = c1.getId();
System.out.println(testId);
}
/*
* @Test public void testProperties() { assertBasicGetterSetterBehavior(new
* Cliente()); }
*/
@Test
public void testValidatorUpdate() {
String shortString = "x";
Cliente c = clienteDAO.findById(testId);
c.setNombre(null);
c.setTelefono(shortString);
clienteDAO.update(c);
}
@Test
public void testValidatorInsert() {
String shortString = "x";
Cliente d = new Cliente();
d.setNombre(null);
d.setTelefono(shortString);
clienteDAO.insert(d);
}
@Test
public void testModificar() {
Cliente a = clienteDAO.findById(testId);
a.setDireccion("Santa Claus");
clienteDAO.update(a);
Cliente c = clienteDAO.findById(testId);
assertEquals("Santa Claus", c.getDireccion());
}
@Test
public void testFindByNombre() {
List<Cliente> cList = clienteDAO.findByNombre("%clai%");
assertEquals(1, cList.size());
Cliente c = cList.get(0);
assertEquals("555", c.getTelefono());
}
@Test
public void testFindAll() {
List<Cliente> cList = clienteDAO.findAll();
assertEquals(3, cList.size());
}
}
//--------------------------------------------------------
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.hormiga</groupId>
<artifactId>jpizza</artifactId>
<name>jpizza</name>
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.hormiga.ui.FrmApp</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4-collab-SNAPSHOT</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<!--
<repositories>
<repository>
<releases>
<enabled>false</enabled>
</releases>
<snapshots />
<id>apache.org</id>c
<name>Maven Snapshots</name>
<url>http://people.apache.org/repo/m2-snapshot-repository</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<enabled>false</enabled>
</releases>
<snapshots />
<id>apache.org</id>
<name>Maven Plugin Snapshots</name>
<url>http://people.apache.org/repo/m2-snapshot-repository</url>
</pluginRepository>
</pluginRepositories>
-->
<dependencies>
<!--<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency> -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.5</version>
<classifier>jdk15</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>concurrent</groupId>
<artifactId>concurrent</artifactId>
<version>1.3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jpa</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-dao</artifactId>
<version>2.0.7</version>
</dependency>
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.7</version>
</dependency> -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<!-- <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>3.0.0.ga</version>
</dependency>-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.3.1.ga</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.5.ga</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>3.0.0.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>