Hibernate @Formula

3 respostas
D

Pessoal, como faço para o @Formula funcionar? Os dados são inseridos na tabela, mas, quando mando listar todas as tuplas, o atributo anotado com o @Formula vem nulo.

Código utilizado:

@Entity

@org.hibernate.annotations.Entity(mutable = false)

@Table(name = employee)

public class Employee implements Serializable {

public Employee() {
}

@Id
@Column(name = "id")
Integer id;

@Column(name = "name")
String name;

@Column(name = "total")
BigDecimal total;

@Column(name = "tax_rate")
BigDecimal taxRate;

@Formula("TOTAL + TAX_RATE * TOTAL")
BigDecimal totalIncludingTaxRate;

public BigDecimal getTotalIncludingTaxRate() {
	return totalIncludingTaxRate;
}

public BigDecimal getTotal() {
	return total;
}

public void setTotal(BigDecimal total) {
	this.total = total;
}

public BigDecimal getTaxRate() {
	return taxRate;
}

public void setTaxRate(BigDecimal taxRate) {
	this.taxRate = taxRate;
}

public Integer getId() {
	return id;
}

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

public String getName() {
	return name;
}

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

public void setTotalIncludingTaxRate(BigDecimal totalIncludingTaxRate) {
	this.totalIncludingTaxRate = totalIncludingTaxRate;
}

}

package hello;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

public class Example1 {

/**
 * @param args
 */
public static void main(String[] args) {
	/** Getting the Session Factory and session */
	SessionFactory session = HibernateUtil.getSessionFactory();
	Session sess = session.getCurrentSession();
	/** Starting the Transaction */
	Transaction tx = sess.beginTransaction();
	/** Creating Pojo */

	for (int i = 1; i < 6; i++) {
		Employee pojo = new Employee();
		pojo.setId(new Integer(i));
		pojo.setName("XYZ");
		pojo.setTotal(BigDecimal.valueOf((100 * i)));
		pojo.setTaxRate(BigDecimal.valueOf(0.1));
		/** Saving POJO */
		sess.save(pojo);
	}

	List<Employee> l1 = sess.createQuery("from Employee").list();

	[b]for (Employee l : l1) {
		System.out.println(l.getId() + " - " + l.getTotalIncludingTaxRate());
	}[/b]
	
	/** Commiting the changes */
	tx.commit();

	session.close();

}

}

CONSOLE:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).

log4j:WARN Please initialize the log4j system properly.

Hibernate:

insert

into

employee

(name, tax_rate, total, id)

values

(?, ?, ?, ?)

Hibernate:

insert

into

employee

(name, tax_rate, total, id)

values

(?, ?, ?, ?)

Hibernate:

insert

into

employee

(name, tax_rate, total, id)

values

(?, ?, ?, ?)

Hibernate:

insert

into

employee

(name, tax_rate, total, id)

values

(?, ?, ?, ?)

Hibernate:

insert

into

employee

(name, tax_rate, total, id)

values

(?, ?, ?, ?)

Hibernate:

select

employee0_.id as id0_,

employee0_.name as name0_,

employee0_.tax_rate as tax3_0_,

employee0_.total as total0_,

employee0_.TOTAL + employee0_.TAX_RATE * employee0_.TOTAL as formula0_

from

employee employee0_

1 - null
2 - null
3 - null
4 - null
5 - null

3 Respostas

romarcio

Nunca usei essa anotação, mas tenta colocar um select dentro da anotação
@formula(“SELECT valor_1 + valor_2 * valor_3 FROM tabela”) ou como vc tinha feito, mas utilizar os atributos criados e não as colunas.

D

Não funcionou, tem que ser o nome da coluna mesmo :-/

G

Conseguiram achar a solução?

Eu coloquei um clear() depois dos inserts e passou a funcionar para mim, mas esta nao é a solução ideal, tem alguma coisa a ver com o cache (apesar de executar a select).

Qualquer outra solução postem aqui ok.

Criado 27 de fevereiro de 2011
Ultima resposta 3 de abr. de 2012
Respostas 3
Participantes 3