Fetch data from multiple tables using eclipselink - eclipselink

This is in response to my question titled "Join tables using eclipselink" asked yesterday.
I will put my scenario here. I have three tables auction, items. one auction can have many items. the mapping between these two tables is through a third table called auctionitems. Below are my tables
CREATE TABLE AUCTION (
auction_id integer,
min_bid_amt decimal(15,2),
max_bid_amt decimal(15,2),
auction_start_ts timestamp,
auction_end_ts timestamp,
owner_id varchar(30),
create_ts timestamp,
last_updt_ts timestamp
);
ALTER TABLE AUCTION ADD CONSTRAINT auction_auction_id_pk PRIMARY KEY(auction_id);
CREATE TABLE ITEM (
item_id integer,
item_name varchar(30),
item_desc varchar(50),
item_image_id integer,
create_ts timestamp,
last_updt_ts timestamp
);
ALTER TABLE ITEM ADD CONSTRAINT item_item_id_pk PRIMARY KEY(item_id);
CREATE TABLE AUCTIONITEMS (
item_id integer,
auction_id integer,
create_ts timestamp,
last_updt_ts timestamp
);
ALTER TABLE AUCTIONITEMS ADD CONSTRAINT item_item_id_fk FOREIGN KEY(item_id) REFERENCES ITEM(item_id);
ALTER TABLE AUCTIONITEMS ADD CONSTRAINT auction_auction_id_fk FOREIGN KEY(auction_id) REFERENCES AUCTION(auction_id);
ALTER TABLE AUCTIONITEMS ADD CONSTRAINT auctionitems_pk_1 PRIMARY KEY(item_id,auction_id);
Now I want to display item_name, item_desc, auction_start_ts and auction_end_ts in my jsp page for all auctions(lets say for 5 auctions).
I am using eclipse link as JPA provider, Tomcat 6.0.29, Spring 3.0.5 and MYSQL 5.1.
These are my domain classes.
Auction :
package com.persistent.eap.domain;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.*;
#Entity
#Table(name="Auction")
public class Auction implements Serializable
{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private long auction_Id;
private double min_Bid_Amt;
private double max_Bid_Amt;
private Timestamp auction_Start_Ts;
private Timestamp auction_End_Ts;
private long owner_Id;
private Timestamp create_Ts;
private Timestamp last_Updt_Ts;
public long getAuction_Id() {
return auction_Id;
}
public void setAuction_Id(long auctionId) {
auction_Id = auctionId;
}
public double getMin_Bid_Amt() {
return min_Bid_Amt;
}
public void setMin_Bid_Amt(double minBidAmt) {
min_Bid_Amt = minBidAmt;
}
public double getMax_Bid_Amt() {
return max_Bid_Amt;
}
public void setMax_Bid_Amt(double maxBidAmt) {
max_Bid_Amt = maxBidAmt;
}
public Timestamp getAuction_Start_Ts() {
return auction_Start_Ts;
}
public void setAuction_Start_Ts(Timestamp auctionStartTs) {
auction_Start_Ts = auctionStartTs;
}
public Timestamp getAuction_End_Ts() {
return auction_End_Ts;
}
public void setAuction_End_Ts(Timestamp auctionEndTs) {
auction_End_Ts = auctionEndTs;
}
public long getOwner_Id() {
return owner_Id;
}
public void setOwner_Id(long ownerId) {
owner_Id = ownerId;
}
public Timestamp getCreate_Ts() {
return create_Ts;
}
public void setCreate_Ts(Timestamp createTs) {
create_Ts = createTs;
}
public Timestamp getLast_Updt_Ts() {
return last_Updt_Ts;
}
public void setLast_Updt_Ts(Timestamp lastUpdtTs) {
last_Updt_Ts = lastUpdtTs;
}
}
Item :
package com.persistent.eap.domain;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.*;
#Entity
#Table(name="Item")
public class Item implements Serializable
{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private long item_Id;
private String item_Name;
private String item_Desc;
private int item_Image_Id;
private Timestamp create_ts;
private Timestamp last_updt_ts;
public long getItem_Id() {
return item_Id;
}
public void setItem_Id(long itemId) {
item_Id = itemId;
}
public String getItem_Name() {
return item_Name;
}
public void setItem_Name(String itemName) {
item_Name = itemName;
}
public String getItem_Desc() {
return item_Desc;
}
public void setItem_Desc(String itemDesc) {
item_Desc = itemDesc;
}
public int getItem_Image_Id() {
return item_Image_Id;
}
public void setItem_Image_Id(int itemImageId) {
item_Image_Id = itemImageId;
}
public Timestamp getCretae_ts() {
return create_ts;
}
public void setCretae_ts(Timestamp cretaeTs) {
create_ts = cretaeTs;
}
public Timestamp getLast_updt_ts() {
return last_updt_ts;
}
public void setLast_updt_ts(Timestamp lastUpdtTs) {
last_updt_ts = lastUpdtTs;
}
}
AuctionItems:
package com.persistent.eap.domain;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Entity;
#Entity
public class AuctionItems implements Serializable{
private static final long serialVersionUID = 1L;
private int itemId;
private int auctionId;
private Timestamp createTs;
private Timestamp lastUpdtTs;
public AuctionItems(){
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public int getAuctionId() {
return auctionId;
}
public void setAuctionId(int auctionId) {
this.auctionId = auctionId;
}
public Timestamp getCreateTs() {
return createTs;
}
public void setCreateTs(Timestamp createTs) {
this.createTs = createTs;
}
public Timestamp getLastUpdtTs() {
return lastUpdtTs;
}
public void setLastUpdtTs(Timestamp lastUpdtTs) {
this.lastUpdtTs = lastUpdtTs;
}
}
In AuctionItems there is no #Id because it is a composite primary key. I do not know how to put a composite primary key. And JPA complains that this class is not a known Entity type.
And finally my persistence.xml file
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<persistence-unit name="portalintegrator" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.persistent.eap.domain.UserDetails</class>
<class>com.persistent.eap.domain.Item</class>
<class>com.persistent.eap.domain.Auction</class>
<properties>
<property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/portaldemo" />
<property name="eclipselink.jdbc.user" value="root" />
<property name="eclipselink.jdbc.password" value="root" />
<property name="eclipselink.ddl-generation" value="None" />
<property name="eclipselink.logging.level" value="INFO" />
<property name="eclipselink.target-database" value="MYSQL" />
</properties>
</persistence-unit>
</persistence>
I have a couple of questions?
If I use JPA annotations how would I achieve this?
If I use JPQL what is the query going to look like to fetch the above 4 fields?

Normally you would use a #ManyToMany mapping to map a join table, your Auction would have an items field of List.
See,
http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany
You join table seems a little more complex, with additional fields, so you may also want to map it an an Entity, see,
http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns

Related

Spring-data-solr config

i met a problem in Studying with Spring data solr,this is my Configuration Class:
#Configuration
#EnableSolrRepositories(basePackages={"cn.likefund.solr.repository"}, multicoreSupport=true)
public class SolrContext {
static final String SOLR_HOST = "http://192.168.11.157:8080/solr";
#Bean
public SolrClient solrClient() {
return new HttpSolrClient(SOLR_HOST);
}
}
and this is my Repository:
package cn.likefund.solr.repository;
import java.util.List;
import org.springframework.data.solr.repository.SolrCrudRepository;
import cn.likefund.solr.model.Activity;
public interface ActivityRepository extends SolrCrudRepository<Activity, String>{
List<Activity> findByName(String name);
}
when I start the application,the message in console is this
error
When I delete the method findByName in the repository,the application start with no problem, i just want to the method findByName worked,anybody know what should i do with this problem?
here is the Activity Class:
#Entity
#SolrDocument(solrCoreName ="core_activity")
public class Activity implements Serializable{
private static final long serialVersionUID = 1566434582540525979L;
#Id
#Field(value = "id")
private String id;
#Field(value = "CREATEDT")
private String createdt;
#Indexed
#Field(value = "NAME")
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCreatedt() {
return createdt;
}
public void setCreatedt(String createdt) {
this.createdt = createdt;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
So, obviously the CrudRepository is not created .
when you delete the findByName, can you manually query your repo ? (just to be sure the problem comes from the method, and not the SOLR schema)
have you tried to annotate annotate the method to explicitly set the query ? Something like
#Query("NAME:?0")
List findByName(String name);

Hibernate queryexception: could not resolve entity property during JPA query

I am trying to query my hibernate table for a RunEntity. The first where clause in the query searches for RunEntities where the testName = the passed value testname. In the stacktrace, it mentions that it cannot find a match for type testname in the RunEntity, but the RunEntity object explicitly has a string named testName with setters and getters and #Column notation.
Table setup
CREATE TABLE RunEntity (ID INTEGER IDENTITY,TestNumber INTEGER NOT NULL, TestName varchar(50) NOT NULL, ENVIRONMENT VARCHAR(50) NOT NULL, Source VARCHAR(50), Date TIMESTAMP, RESULTFILES BLOB);
Query
#Query("SELECT r FROM RunEntity r WHERE r.testName = :testname AND r.testNumber = :testnumber AND r.environment = :environment AND r.source = :source")
public List<RunEntity> findByNameNumberEnvironmentSource(
#Param("testname") String testname,
#Param("testnumber") int testnumber,
#Param("environment") String environment,
#Param("source") String source);
Entity
package com.web_application;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Lob;
#Entity
#Table(name = "TESTRUNS")
public class RunEntity {
private int ID;
private int testNumber;
private String testName;
private String environment;
private String source;
private String passOrFail;
private Timestamp date;
private byte[] resultFiles;
#Id
#Column(name = "ID")
#GeneratedValue
public int getID()
{
return this.ID;
}
public void setID(int ID){this.ID = ID;}
#Column(name="TestNumber")
public int getTestNumber()
{
return this.testNumber;
}
public void setTestNumber(int testNum){this.testNumber = testNum;}
#Column(name="TestName")
public String testName()
{
return this.testName;
}
public void setTestName(String testName){this.testName = testName;}
#Column(name="Environment")
public String getEnvironment()
{
return this.environment;
}
public void setEnvironment(String enviro){this.environment = enviro;}
#Column(name="Source")
public String getSource()
{
return this.source;
}
public void setSource(String src){this.source = src;}
#Column(name="PassOrFail")
public String getPassOrFail()
{
return this.passOrFail;
}
public void setPassOrFail(String pOrF){this.passOrFail = pOrF;}
#Column(name="Date")
public Timestamp getDate()
{
return this.date;
}
public void setDate(Timestamp dates){this.date = dates;}
#Lob
#Column(name="ResultFiles")
public byte[] getResultFiles()
{
return this.resultFiles;
}
public void setResultFiles(byte[] file){this.resultFiles = file;}
}
Part of stacktrace
Caused by: org.hibernate.QueryException: could not resolve property: testname of: com.web_application.RunEntity [SELECT r FROM com.web_application.RunEntity r WHERE r.testname = :testname AND r.testNumber = :testnumber AND r.environment = :environment AND r.source = :source]
at org.hibernate.QueryException.generateQueryException(QueryException.java:137)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
... 66 more
Change this
#Column(name="TestName")
public String testName()
{
return this.testName;
}
to
#Column(name="TestName")
public String getTestName()
{
return this.testName;
}
Property access Naming convention is important.Try to use IDE for example (Eclipse Getter-Setter,instead using manually doing it)
Correct your testName() getter to getTestName(). You are using Property Access and have to stick to JavaBeans convention.

PersistenceException: ERROR executing DML bindLog when Updating an Object

Good day! I have two objects: Tag and RelatedTag. The Tag can have many RelatedTags (which is also a Tag). Saving the Tag with its related tags works fine. But when I update the Tag, it has an error saying
[PersistenceException: ERROR executing DML bindLog[] error[Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.RELATED_TAG(ID)"; SQL statement:\n insert into related_tag (id, tag_id, relationship, related_notes) values (?,?,?,?) [23505-172]]]
Here is Tag model:
package models;
import java.util.*;
import javax.persistence.*;
import javax.validation.*;
import play.data.Form;
import play.data.validation.Constraints.*;
import play.db.ebean.*;
import play.db.ebean.Model.Finder;
import scala.Int;
#Entity
public class Tag extends Model{
#Id
private int id;
#Required
#MaxLength(value=100)
private String name;
#MaxLength(value=200)
private String notes;
#OneToMany(cascade=CascadeType.ALL)
public List<RelatedTag> relatedTags = new ArrayList<RelatedTag>();
public static Finder<Integer, Tag> find = new Finder(Int.class, Tag.class);
public Tag() {
}
public Tag(String name, String notes){
this.name = name;
this.notes = notes;
}
public Tag(int id, String name, String notes, List<RelatedTag> relatedTags) {
this.id = id;
this.name = name;
this.notes = notes;
this.relatedTags = relatedTags;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public List<RelatedTag> getRelatedTags() {
return relatedTags;
}
public void setRelatedTags(List<RelatedTag> relatedTags) {
this.relatedTags = relatedTags;
}
public static List<Tag> all() {
return find.all();
}
public static void create(Tag tag){
tag.save();
}
public static void delete(int id){
find.ref(id).delete();
}
public static void update(int id, Tag tag) {
tag.update(id); // updates this entity, by specifying the entity ID
}
public static boolean exists(Tag newTag) {
for(Tag allTags : Tag.find.all()) {
if(allTags.getName().equals(newTag.getName()))
return true;
}
return false;
}
}
And here is the RelatedTag model:
package models;
import java.util.*;
import javax.persistence.*;
import javax.validation.*;
import play.data.Form;
import play.data.validation.Constraints.*;
import play.db.ebean.*;
import play.db.ebean.Model.Finder;
import scala.Int;
#Entity
public class RelatedTag extends Model {
#Id
private int id;
private String relationship;
private String relatedNotes;
public RelatedTag() {}
public RelatedTag(int id, String relationship, String relatedNotes) {
this.id = id;
this.relationship = relationship;
this.relatedNotes = relatedNotes;
}
public void setId(int id){
this.id = id;
}
public void setRelationship(String relationship){
this.relationship = relationship;
}
public void setRelatedNotes(String relatedNotes) {
this.relatedNotes = relatedNotes;
}
public int getId(){
return id;
}
public String getRelationship(){
return relationship;
}
public String getRelatedNotes() {
return relatedNotes;
}
public static boolean exists(String tagRelated) {
for(Tag tag : Tag.find.all()) {
if(tagRelated.equals(tag.getName()))
return true;
}
return false;
}
public static RelatedTag findByLabel(String tagRelated, String relation, String relatedNotes) {
RelatedTag relatedTag = null;
for(Tag tag : Tag.find.all()) {
if(tagRelated.equals(tag.getName())) {
relatedTag = new RelatedTag(tag.getId(), relation, relatedNotes);
}
}
return relatedTag;
}
public static Tag findTag(int id) {
for(Tag tag : Tag.find.all()) {
if(id == tag.getId())
return tag;
}
return null;
}
}
When I run this (in which I update a Tag), the error happens.
private static void reciprocate(Tag tag) {
List<Tag> peers = new ArrayList<Tag>();
for (RelatedTag rt : tag.getRelatedTags()) {
if(rt.getRelationship().equals("peer"))
peers.add(RelatedTag.findTag(rt.getId()));
}
for(RelatedTag rt : tag.getRelatedTags()) {
int relTemp = 0;
String relation = new String();
if (rt.getRelationship().equals("parent"))
relTemp = 1;
if (rt.getRelationship().equals("child"))
relTemp = 2;
if (rt.getRelationship().equals("peer"))
relTemp = 3;
switch(relTemp) {
case 1: relation = "child"; break;
case 2: relation = "parent"; break;
case 3: relation = "peer"; break;
}
Tag related = new Tag();
related = Tag.find.byId(RelatedTag.findTag(rt.getId()).getId());
List<RelatedTag> available = new ArrayList<RelatedTag>();
List<String> availableName = new ArrayList<String>();
for (RelatedTag rt2 : related.getRelatedTags()) {
availableName.add(RelatedTag.findTag(rt2.getId()).getName());
}
if(availableName.contains(tag.getName())) {
for(RelatedTag rt2 : related.getRelatedTags()) {
if(!RelatedTag.findTag(rt2.getId()).getName().equals(tag.getName())) {
available.add(rt2);
}
}
}
available.add(RelatedTag.findByLabel(
tag.getName(), relation,
rt.getRelatedNotes()));
related.setRelatedTags(available);
related.update(related.getId()); //HERE
}
}
Please help me figure this out. After the first rt has been iterated, there goes the error but it saves its related tag. Thank you very much.
Your method RelatedTag#findByLabel always creates new RelatedTags with the IDs of the Tag class; if you have 2 related tags for the same tag, it will produce 2 related tags with the same ID.
Look into #GeneratedValue and EntityManager#createQuery.

Error in hibernate call using annotations

I am getting this error
menu_categories is not mapped [from menu_categories]
my hibernate call is
public List loadMenuCategories(SessionFactory sessionFactory){
List types = new ArrayList<MenuCategories>();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("from menu_categories");
List result = query.list();
Iterator it = result.iterator();
while(it.hasNext()){
MenuCategories menuCategories = (MenuCategories)it.next();
types.add(menuCategories);
}
sessionFactory.close();
return types;
}
and my bean is
#Entity
#Table(appliesTo = "menu_categories")
public class MenuCategories extends BaseModel{
/**
*
*/
private static final long serialVersionUID = -4875305890823765933L;
}
package com.rizstien.myhotel.framework.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.apache.commons.lang.StringUtils;
public class BaseModel implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id", nullable=false)
private Integer id;
#Column(name = "name")
private String name;
#Column(name = "description")
private String desc;
#Column(name = "is_active")
private boolean active;
#Column(name = "no_of_items")
private Integer noOfItems;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
if (!StringUtils.isEmpty(name)) {
this.name = name;
}
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
if (!StringUtils.isEmpty(desc)) {
this.desc = desc;
}
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public Integer getNoOfItems() {
return noOfItems;
}
public void setNoOfItems(Integer noOfItems) {
this.noOfItems = noOfItems;
}
}
EDIT
this is my hibernate config file
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3311/myhotel</property>
<property name="hibernate.connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">5</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.rizstien.myhotel.menucategories.model.MenuCategories"/>
</session-factory>
The query you're executing is not SQL. It's HQL. HQL queries entities, not tables. It should thus be from MenuCategories. This entity, BTW, should be named MenuCategory, sicne one instance of it represent one category, and not several categories.
Read the documentation.
I had mentioned db name in annotation and it solved the problem
#Entity
#Table(name = "menu_categories", catalog="db_name")
public class MenuCategories extends BaseModel{
private static final long serialVersionUID = -4875305890823765933L;
}

Creating Envers custom revision entity

I'm trying to setup audit for our project.
I started from the default configuration which works fine.
The next step is to store the user which has made changes.
Following the manual I created custom entity revision:
package com.csbi.samples.utils.audit;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;
import org.hibernate.envers.RevisionNumber;
import org.hibernate.envers.RevisionTimestamp;
import org.hibernate.envers.RevisionEntity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
#Entity
#Table(name="REVISIONS")
#RevisionEntity(CustomRevisionListener.class)
public class CustomRevisionEntity implements Serializable {
private static final long serialVersionUID = -1255842407304508513L;
#Id
#GeneratedValue
#RevisionNumber
private int id;
#RevisionTimestamp
private long timestamp;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#Transient
public Date getRevisionDate() {
return new Date(timestamp);
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public boolean equals(Object o) {
if(this == o) return true;
if(!(o instanceof CustomRevisionEntity)) return false;
CustomRevisionEntity that = (CustomRevisionEntity) o;
if(id != that.id) return false;
if(timestamp != that.timestamp) return false;
if(timestamp != that.timestamp) return false;
if(username != that.username) return false;
return true;
}
public int hashCode() {
int result;
result = id;
result = 31 * result + (int) (timestamp ^ (timestamp >>> 32));
return result;
}
public String toString() {
return "DefaultRevisionEntity(user = " + username + "id = " + id + ", revisionDate = " + DateFormat.getDateTimeInstance().format(getRevisionDate()) + ")";
}
}
And also custom listener:
package com.csbi.samples.audit;
import org.hibernate.envers.RevisionListener;
public class CustomRevisionListener implements RevisionListener {
public void newRevision(Object revisionEntity) {
CustomRevisionEntity revision = (CustomRevisionEntity) revisionEntity;
revision.setUsername("username"); //for testing
}
}
Here is some lines from log:
DEBUG: org.hibernate.envers.configuration.metadata.AuditMetadataGenerator -
Generating first-pass auditing mapping for entity
com.csbi.samples.domain.Property.
DEBUG:
org.hibernate.envers.configuration.metadata.AuditMetadataGenerator -
Generating second-pass auditing mapping for entity
com.csbi.samples.domain.Property.
INFO : org.hibernate.cfg.HbmBinder
- Mapping class: com.csbi.samples.domain.Property_AUD -> PROPERTIES_AUD
INFO : org.hibernate.cfg.HbmBinder - Mapping class:
org.hibernate.envers.DefaultRevisionEntity -> REVINFO
Take a look at the last line of the output.
There is still DefaultRevisionEntity mapped instead of CustomRevisionEntity.
I have no idea what is wrong. Any suggestions?
Solved. Entity is not in scanned by Hibernate directory.