-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: validator don't work in merge method
PostPosted: Tue Jan 15, 2008 6:49 pm 
Newbie

Joined: Tue Jan 15, 2008 6:34 pm
Posts: 3
I really do not have error simply does not work the validation to me

Attempt to use validador with annotations @NotNull and works for the insertion

-- entityManager.persist(customer);

but it does not in update process.

-- entityManager.merge(customer);


i use hibernate-entitymanager version 3.3.1.ga

any ideas, that I make bad


sorry for my english


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 15, 2008 9:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I dunno, there is a unit test for that. You will have to describe things further.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 16, 2008 10:05 am 
Newbie

Joined: Tue Jan 15, 2008 6:34 pm
Posts: 3
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>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 18, 2008 7:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: org.hormiga.domain.Cliente.nombre

is actually not an Hibernate Validator exception, that's an Hibernate Core one that happens before HV kicks in.

You'll have to try the debugger because I don't see any reason, unless the spring configuration for the event listeners is wrong. I am not familiar with it though (my configurations are at least 10 times smaller thanks to that ;) )

_________________
Emmanuel


Top
 Profile  
 
 Post subject: there is a buy ?
PostPosted: Sun Jan 20, 2008 4:56 pm 
Newbie

Joined: Tue Jan 15, 2008 6:34 pm
Posts: 3
Enmanuel:

adding "@EntityListeners(org.hibernate.validator.event.JPAValidateListener.class)" in my entity, i change my error from "javax.persistence.PersistenceException: org.hibernate.PropertyValueException" to "org.hibernate.validator.InvalidStateException" (this is correct) , i thought that it is not necessary, i'm wrong .

but, i think there is a bug , if i call isolated em.merge(), event validator from hibernate is not fired (don't work), nevertheless when I call em.flush() immediately after em.merge(), event validator is fired (ok)

¿?


Top
 Profile  
 
 Post subject: Re: there is a buy ?
PostPosted: Thu Jan 31, 2008 12:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
a.gallen wrote:
but, i think there is a bug , if i call isolated em.merge(), event validator from hibernate is not fired (don't work), nevertheless when I call em.flush() immediately after em.merge(), event validator is fired (ok)


Ah this is not a bug, this is perfectly expected.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.