Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 365011 - False validation error for entity attributes that are matching ID class attribute
Summary: False validation error for entity attributes that are matching ID class attri...
Status: RESOLVED FIXED
Alias: None
Product: Dali JPA Tools
Classification: WebTools
Component: JPA (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P3 major (vote)
Target Milestone: 3.0.2   Edit
Assignee: Nan Li CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-28 17:02 EST by Nan Li CLA
Modified: 2012-02-15 14:47 EST (History)
3 users (show)

See Also:
neil.hauge: review+


Attachments
sample project (5.34 KB, application/octet-stream)
2011-11-28 17:04 EST, Nan Li CLA
neil.hauge: iplog-
Details
Proposed Patch (1.40 KB, patch)
2012-01-20 15:45 EST, Nan Li CLA
neil.hauge: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Nan Li CLA 2011-11-28 17:02:10 EST
Build Identifier: I-3.4.0-20111117042436

Create an entity like the one below and corresponding ID class.

@IdClass(TestPK.class)
@Access(AccessType.PROPERTY)
public class Test implements Serializable {

	@Transient
	private int id_1;
	private int id_2;
	private static final long serialVersionUID = 1L;

	public Test() {
		super();
	}
	
	@Id
	public int getId_1() {
		return this.id_1;
	}

	public void setId_1(int id_1) {
		this.id_1 = id_1;
	}  
	
	@Id
	public int getId_2() {
		return this.id_2;
	}

	public void setId_2(int id_2) {
		this.id_2 = id_2;
	}
   
}

A sample project is attached. An error message is given complaining "The attribute matching the ID class attribute id_1 is not mapped as a primary key."; however, the corresponding attribute id_1 of the entity is indeed mapped as primary key on the property. The problem is we try to count all the mapped attributes, the properties and annotated fields in this case (entity has property access type), including transient ones for the entity. It ends up with two attributes same name matching the corresponding ID attribute and one of them is annotated as transient so when doing the validation we try to go through each of them to check whether the matching ones are annotated as primary key and give the error message when finding the transient one is not.

Reproducible: Always
Comment 1 Nan Li CLA 2011-11-28 17:04:39 EST
Created attachment 207630 [details]
sample project
Comment 2 Karen Butzke CLA 2011-12-06 12:51:55 EST
I can also get this invalid error with this use case:
@Entity
@IdClass(DependentId.class)
@Access(AccessType.FIELD)
public class Dependent {
	@Transient
	String name;

	@Id
	@ManyToOne
	Employee emp;

	@Id
	@Access(AccessType.PROPERTY)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

This might be a more valid use case, the one Nan gave has @Transient defined on the id_1 field when it's not necessary to define it. I'm bumping this to major since it results in invalid validation
Comment 3 Nan Li CLA 2012-01-20 15:45:26 EST
Created attachment 209857 [details]
Proposed Patch
Comment 4 Neil Hauge CLA 2012-01-24 16:52:46 EST
Patch committed to maintenance and head for M5.
Comment 5 Neil Hauge CLA 2012-01-24 17:13:51 EST
.
Comment 6 Nikolay Alexiev CLA 2012-02-15 14:24:18 EST
I get the same error for the following relatively simple case:
Entity:

@IdClass(CompositeId.class)
@Entity
@Cacheable(false)
public class Album extends ContentItem implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private String title;

    @Id
    private String ancestor;

    private String album;
}

Id:

public class Composite implements Serializable {

    private static final long serialVersionUID = 1L;

    private String title;

    private String ancestor;
}

The error is: "The attribute matching the ID class attribute title is not mapped as a primary key"

What is strange to me is the fact that if I define the Id class in the same file as the Entity everything is just fine.
Unfortunately that is not a possible workaround since the id class can't be used outside the package and I can't write my unit tests.
Comment 7 Neil Hauge CLA 2012-02-15 14:47:54 EST
(In reply to comment #6)
As a workaround, you could disable this error message in the JPA preferences at the workspace or project level.  Currently this fix is only in the Juno release.