Strange issue on @ManyToOne
ajevans85 Nov 2, 2010 8:58 AMI am using Hibernate 3.5.6-Final and prototyping envers for a project and have some strange behaviour (or what looks like strange behaviour based on my understanding).
I have an jpa entity Activity
{code}
@Entity
@Table(name = "activity")
@Audited
public class Activity {
...
@JoinColumn(name = "location_id", referencedColumnName = "id", nullable = false)@ManyToOne(optional = false)
private Location location
...}
{code}
And also class Location
{code}
@Entity
@Table(name = "location")
@Audited
public class Location {
...@OneToMany(cascade = CascadeType.ALL, mappedBy = "location")
private List<Activity> activityList = new ArrayList<Activity>();
...}
{code}
I have some tests setup
I insert a sample row of data using sql into the Activity and Location table.
I assert that number of revisions for activity = 0 which passes
I make a update to a activity and persist it using jpa
I then get the list of revisions and attempt to retrieve the first revision from the list
I get a EntityNotFoundException on the location entity
Looking through the query logs it retrieves a activity with _revision 3
{code:sql}
select
activity_v0_.id as id38_,
activity_v0_._revision as column2_38_,
_rev_type as column3_38_,
activity_v0_.accreditation as accredit4_38_,
activity_v0_.activity_type as activity5_38_,
activity_v0_.advance_reg_required as advance6_38_,
activity_v0_.contact_email as contact8_38_,
activity_v0_.contact_name as contact9_38_,
activity_v0_.contact_phoneas contact10_38_,
activity_v0_.cost_information as cost11_38_,
activity_v0_.created as created38_,
activity_v0_.description as descrip13_38_,
activity_v0_.difficulty as difficulty38_,
activity_v0_.external_id as external15_38_,
activity_v0_.factsheet_filename as factsheet16_38_,
activity_v0_.factsheet_url as factsheet17_38_,
activity_v0_.family_friendly as family18_38_,
activity_v0_.is_free as is19_38_,
activity_v0_.meeting_point as meeting20_38_,
activity_v0_.prevent_xml_update as prevent21_38_,
activity_v0_.priority as priority38_,
activity_v0_.reg_details as reg23_38_,
activity_v0_.short_description as short24_38_,
activity_v0_.skill_level as skill25_38_,
activity_v0_.slug as slug38_,
activity_v0_.special_requirements as special27_38_,
activity_v0_.status as status38_,
activity_v0_.tagline as tagline38_,
activity_v0_.title as title38_,
activity_v0_.updated as updated38_,
activity_v0_.url as url38_,
activity_v0_.walk_distance as walk33_38_,
activity_v0_.location_id as location34_38_,
activity_v0_.organiser_partner_id as organiser35_38_,
activity_v0_.owner_partner_id as owner36_38_
from
activity_versions activity_v0_
where
_rev_type<>1
and
_revision=(
select max(_revision) from activity_versions activity_v1_
where
_revision<=3 and activity_v0_.id=activity_v1_.id
)
and activity_v0_.id=9
{code}
The below is the location query which is run but returns no results
{code:sql}
select
location_v0_.id as id35_,
location_v0_._revision as column2_35_,
_rev_type as column3_35_,
location_v0_.alt_name as alt4_35_,
location_v0_.created as created35_,
location_v0_.description as descript6_35_,
location_v0_.external_id as external7_35_,
location_v0_.name as name35_,
location_v0_.prevent_xml_update as prevent9_35_,
location_v0_.slug as slug35_,
location_v0_.status as status35_,
location_v0_.updated as updated35_,
location_v0_.owner_partner_id as owner13_35_
from
location_versions location_v0_
where
_rev_type<>2 and _revision=(
select max(_revision) from location_versions location_v1_
where
_revision<=3 and location_v0_.id=location_v1_.id
)
and location_v0_.id=5
{code}
In the location versions table I have the below:
{code:sql}select id, _revision, _rev_type FROM location_versions where id = 5;{code}
{code}
+----+-----------+-----------+
| id | _revision | _rev_type |
+----+-----------+-----------+
| 5 | 4 | 1 |
+----+-----------+-----------+
{code}
Looking at the above the javax.persistence.EntityNotFoundException: Unable to find Location with id 5 will be caused because the query is looking for a revision <3 the same as the activity but the location activity only has one revision entry with a value of 4.
Has anyone seen this before or know what I am doing wrong? I'm assuming Envers will be looking for a Location with _revision 3 to match the Activity revision? Is there a way to prevent this?