Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: @javax.persistent.Transient issue..
PostPosted: Mon Sep 19, 2011 1:41 pm 
Newbie

Joined: Sat Dec 25, 2010 9:58 am
Posts: 11
Dear all, Please help to identify the problem with my code.
I've one entity bean like:-

@Entity
@Table(name = "CUSTOMER", uniqueConstraints = @UniqueConstraint(columnNames = {
"USERNAME", "EMAIL" }))
public class Customer implements java.io.Serializable {

private static final long serialVersionUID = 8344606008234672532L;

@NotNull
@UserIdExist(message = "userid already exists. try with different userid")
private BigDecimal id;

@NotEmpty
@Size(min = 1, max = 64)
private String name;

@javax.persistent.Transient
private String myfield;

@email
private String email;
}


Now my Spring MVC controller is :-
@Controller
@RequestMapping("/admin")
public class CustomerController {

@Autowired
private CustomerService customerService;

@RequestMapping("/customer/save")
public String saveCustomer(Customer customer) {
customerService.save(customer);
}

}

My SpringService IMPL is :-

@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDAO customerDAO;

@Override
@Transactional
public void save(Customer customer) {
customerDAO.saveCustomer(customer);
}
}


My springDAO is:-

@Repository
public class CustomerDAOImpl extends LibmsHibernateTemplate implements
CustomerDAO {

@Override
public void saveCustomer(Customer customer) {
getLibmsHibernateTemplate().save(customer); //getLibmsHibernateTemplate is nothing but hibernateTempalte
}
}


Now while i save the customer then @Transient is not ignoring the field. myfield is still occuring in hibernate insert query. As per my understanding it has not to occur since it is declared @javax.persistent.Transient.

Please tell me if i missed out something or there is some problem with Spring using with hibernate.

Thanks.


Top
 Profile  
 
 Post subject: Re: @javax.persistent.Transient issue..
PostPosted: Tue Sep 20, 2011 5:05 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

your configuration does not look complete. For example where is your @Id? Which makes me wonder whether your are actually using JPA or not. Is the Hibernate template approach not using the Hibernate directly w/o JPA? Do you have any additional Hibernate configuration in xml?

--Hardy


Top
 Profile  
 
 Post subject: Re: @javax.persistent.Transient issue..
PostPosted: Tue Sep 20, 2011 5:34 am 
Newbie

Joined: Sat Dec 25, 2010 9:58 am
Posts: 11
Hi! Hardy,

Thanks for the reply. Actually that is a dummy pojo I pasted here. actually you consider @Id on private BigDecimal id; field. and there are respective getters setters. There is not configuration exists in xml. I'm successfully able to persist the data in to database using hibernateTemplate save if I remove the field "myfield" (that i declared @Transient). But i want to keep track of something that's why i placed one new field "myfield" and declared it @Transient because it is not mapped to database table. But while I'm saying hibernateTemplate.save(customer); in log the printed hql is still carrying the "myfield" and throwing error.


Top
 Profile  
 
 Post subject: Re: @javax.persistent.Transient issue..
PostPosted: Tue Sep 20, 2011 5:57 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

if JPA is properly configured and used, @Transient should indeed be honored. Can you create produce a minimal testcase? We need to see the exact classes and configuration which are causing the problem.


Top
 Profile  
 
 Post subject: Re: @javax.persistent.Transient issue..
PostPosted: Tue Sep 20, 2011 10:01 am 
Newbie

Joined: Sat Dec 25, 2010 9:58 am
Posts: 11
Hi Hardy, this is my detailed configuration....

Customer bean:-


package com.libms.domain;

// Generated Oct 2, 2010 12:15:42 PM by Hibernate Tools 3.3.0.GA

import com.libms.custom.validations.EmailIdExist;
import com.libms.custom.validations.NotEmpty;
import com.libms.custom.validations.NotNull;
import com.libms.custom.validations.Size;
import com.libms.custom.validations.UserIdExist;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import org.hibernate.validator.constraints.Email;

/**
* Customer generated by hbm2java
*/
@Entity
@Table(name = "CUSTOMER", uniqueConstraints = @UniqueConstraint(columnNames = {
"USERNAME", "EMAIL" }))
@Matches(field = "password", verifyField = "retypepassword")
public class Customer implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 8344606008234672532L;

@NotNull
@UserIdExist(message = "userid already exists. try with different userid")
private BigDecimal id;

@NotEmpty
@Size(min = 1, max = 64)
private String name;

@Size(min = 1, max = 256)
private String addr;

@NotEmpty
@Size(min = 1, max = 10)
private String dob;

@NotEmpty
@Size(min = 1, max = 30)
private String phone;

@NotEmpty
@Size(min = 1, max = 16)
@UserIdExist(message = "username already exists. try with different username")
private String username;

@NotEmpty
@Size(min = 1, max = 32)
private String password;

@Transient
private String retypepassword;

@NotEmpty
@Email
@EmailIdExist(message = "email ID already exists. try with different email id")
private String email;

private Set<Card> cards = new HashSet<Card>(0);
private Set<Librarian> librarians = new HashSet<Librarian>(0);

public Customer() {
}

public Customer(BigDecimal id) {
this.id = id;
}

public Customer(BigDecimal id, String name, String addr, String dob,
String phone, String username, String password, Set<Card> cards,
Set<Librarian> librarians) {
this.id = id;
this.name = name;
this.addr = addr;
this.dob = dob;
this.phone = phone;
this.username = username;
this.password = password;
this.cards = cards;
this.librarians = librarians;
}

@Id
@Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
public BigDecimal getId() {
return this.id;
}

public void setId(BigDecimal id) {
this.id = id;
}

@Column(name = "NAME", length = 64)
public String getName() {
return this.name;
}

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

@Column(name = "ADDR", length = 256)
public String getAddr() {
return this.addr;
}

public void setAddr(String addr) {
this.addr = addr;
}

@Column(name = "DOB", length = 10)
public String getDob() {
return this.dob;
}

public void setDob(String dob) {
this.dob = dob;
}

@Column(name = "PHONE", length = 30)
public String getPhone() {
return this.phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

@Column(name = "USERNAME", unique = true, length = 16)
public String getUsername() {
return this.username;
}

public void setUsername(String username) {
this.username = username;
}

@Column(name = "PASSWORD", length = 32)
public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer")
public Set<Card> getCards() {
return this.cards;
}

public void setCards(Set<Card> cards) {
this.cards = cards;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer")
public Set<Librarian> getLibrarians() {
return this.librarians;
}

public void setLibrarians(Set<Librarian> librarians) {
this.librarians = librarians;
}

/**
* @return the email
*/
@Column(name = "EMAIL", unique = true, length = 50)
public String getEmail() {
return email;
}

/**
* @param email
* the email to set
*/
public void setEmail(String email) {
this.email = email;
}

/**
* @return the retypepassword
*/
public String getRetypepassword() {
return retypepassword;
}

/**
* @param retypepassword the retypepassword to set
*/
public void setRetypepassword(String retypepassword) {
this.retypepassword = retypepassword;
}

}




Now my Spring MVC controller is :-
@Controller
@RequestMapping("/admin")
public class CustomerController {

@Autowired
private CustomerService customerService;

@RequestMapping("/customer/save")
public String saveCustomer(Customer customer) {
customerService.save(customer);
}
}


My SpringService IMPL is :-

@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDAO customerDAO;

@Override
@Transactional
public void save(Customer customer) {
customerDAO.saveCustomer(customer);
}
}


My springDAO is:-

@Repository
public class CustomerDAOImpl extends LibmsHibernateTemplate implements
CustomerDAO {

@Override
public void saveCustomer(Customer customer) {
getLibmsHibernateTemplate().save(customer); //getLibmsHibernateTemplate is nothing but hibernateTempalte
}
}


My jsp for customer is:-


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<div class="header">
<h2>Customer Create</h2>
<c:if test="${not empty message}">
<div id="message" class="success">${message}</div>
<input type="hidden" id="hidSuccess" name="hidSuccess" />
</c:if>
<c:if test="${error}">
<div id="message" class="error">Form has errors</div>
</c:if>
</div>
<form:form name="createForm" id="createForm" modelAttribute="customer">
<fieldset>

<p>
<form:label path="id">Id <form:errors path="id"
cssClass="error_ui"></form:errors>
</form:label>
<form:input path="id" />

</p>
<p>
<form:label path="username">Login Name <form:errors
path="username" cssClass="error_ui"></form:errors>
</form:label>
<form:input path="username" />
</p>
<p>
<form:label path="email">Email <form:errors path="email"
cssClass="error_ui"></form:errors>
</form:label>
<form:input path="email" />
</p>
<p>
<form:label path="password">Password <form:errors
path="password" cssClass="error_ui"></form:errors>
</form:label>
<form:password path="password" />
</p>
<p>
<!-- <label for="retypepwd">Re Type Password</label><input type="password"
id="rpwd"> -->

<form:label path="retypepassword">Re Type Password <form:errors
path="retypepassword" cssClass="error_ui"></form:errors>
</form:label>
<form:password path="retypepassword" />
</p>
<p>
<form:label path="phone">Phone <form:errors path="phone"
cssClass="error_ui"></form:errors>
</form:label>
<form:input path="phone" />
</p>
</fieldset>
<fieldset>
<p>
<form:label path="addr">Address <form:errors path="addr"
cssClass="error_ui"></form:errors>
</form:label>
<form:textarea path="addr" />
</p>
<p>
<form:label path="name">Name <form:errors path="name"
cssClass="error_ui"></form:errors>
</form:label>
<form:input path="name" />
</p>
<p>
<form:label path="dob">Date Of Birth <form:errors
path="dob" cssClass="error_ui"></form:errors>
</form:label>
<form:input path="dob" readonly="true" size="8" />
</p>
</fieldset>

<p class="submit">
<c:if test="${empty message}">
<a
href="javascript:saveAndValidateCustomer('divId','validate','createForm');"
class="button pink serif skew glass">Save</a>
</c:if>
</p>

<div id="divId"></div>
<input type="hidden" id="pageName" name="pageName" value="${page}" />
</form:form>
<script>
loadCalendar('dob');
</script>


Hibernate Config xml is :-


<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/b ... ns-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/c ... xt-3.0.xsd">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/libms_schema" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.libms.domain.Customer</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> -->
</props>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>


Please check the retypepassword field. This I declared @Transient but it still persists in to hiebranteTempalate save.
Foolowing is the error occuring



Exception!: org.springframework.dao.InvalidDataAccessResourceUsageException: Could not execute JDBC batch update; SQL [insert into CUSTOMER (ADDR, DOB, EMAIL, NAME, PASSWORD, PHONE, retypepassword, USERNAME, ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update


One more thing I'm serializing the form using JQuery Ajax call

jQuery.ajax({
type : 'post',
url : 'save',
onCreate : showLoader(divId, 'Please wait...'),
data : jQuery("#formName").serialize(),
success : function(html) {
jQuery("#formcontainer").val('');
jQuery("#formcontainer").html(html);
},
error : function(xhr, ajaxOptions, thrownError) {
// alert(xhr.status);
// alert(thrownError);
alert("failed: " + xhr.status + " " + thrownError);
}
});


Top
 Profile  
 
 Post subject: Re: @javax.persistent.Transient issue..
PostPosted: Tue Sep 20, 2011 10:42 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

it seems you are mixing field and property level annotations. Given that your @Id annotation is on the property (getter) only property annotations are considered. You should use a consistent placing of the annotations (at least the javax.persistence ones).

--Hardy


Top
 Profile  
 
 Post subject: Re: @javax.persistent.Transient issue..
PostPosted: Tue Sep 20, 2011 11:13 am 
Newbie

Joined: Sat Dec 25, 2010 9:58 am
Posts: 11
Hardy , thanks a ton... It's working man.

I have one more query, please help me to understand that one too.


As per my understanding HQL is database independent. and just switching the database and appropriate dialect it works on any database. But I encounter with a problem if I use function nvl in HQL, It works fine with oracle database but doesn;t seems to work with mySQL. I replaced nvl with coalsce and problem is fixed.

But my question is since HQL is database independent NVL in hql has to be database independent too.

Please correct me If I'm wrong.


Thanks


Top
 Profile  
 
 Post subject: Re: @javax.persistent.Transient issue..
PostPosted: Wed Sep 21, 2011 4:05 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Have you used coalesce against Oracle. As far as I know coalesce is the official SQL function name and nvl is something Oracle specific.
Looking at the Oracle dialect class it looks like coalesce gets mapped to nvl.

--Hardy


Top
 Profile  
 
 Post subject: Re: @javax.persistent.Transient issue..
PostPosted: Wed Sep 21, 2011 5:38 am 
Newbie

Joined: Sat Dec 25, 2010 9:58 am
Posts: 11
Yeah Hardy I used coalesce against oracle also and it's working fine on oracle as well as mySql. But I understand if functions are database specific in SQL. But if we come to the HQL side, i don't think what ever i written in HQL and working on any database and that will not work on another database by switching the dialect. Actually my query is. NVL in HQL is oracle specific? If yes then HQL can not be considered a database independent one.

Thanks


Top
 Profile  
 
 Post subject: Re: @javax.persistent.Transient issue..
PostPosted: Mon Nov 21, 2011 5:35 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Sorry, I don't understand your last question. Unfortunately, some databases decided to name standardized functions to something database specific. That's where HQL comes in. It maps the standard function names to the database specific ones (within the Dialect). This means you can use the standard function names in HQL and don't care about db specifics.

Once you are a using a non standard function which only exists on a particular database you are of course on your own. Such a usage cannot be portable.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 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.