Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable to put aanotation on private field #691

Closed
ghost opened this issue Aug 13, 2018 · 10 comments
Closed

enable to put aanotation on private field #691

ghost opened this issue Aug 13, 2018 · 10 comments

Comments

@ghost
Copy link

ghost commented Aug 13, 2018

my entity class is

package com.axelor.app.db;

import java.util.List;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "contact")
public class Contact {

	private Long id;

	private String firstName;

	private String lastName;

	private String email;

	private String phone;
	
	private Address mainAddress;
	
	private Address shipAddress;

	private List<Address> addresses;

	public Contact() {
	}

	public Contact(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CONTACT_SEQ")
	@SequenceGenerator(name = "CONTACT_SEQ", sequenceName = "CONTACT_SEQ", allocationSize = 1)
	public Long getId() {
		return id;
	}

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

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

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

	@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	public Address getMainAddress() {
		return mainAddress;
	}
	
	public void setMainAddress(Address mainAddress) {
		this.mainAddress = mainAddress;
	}
	
	@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	public Address getShipAddress() {
		return shipAddress;
	}

	public void setShipAddress(Address shipAddress) {
		this.shipAddress = shipAddress;
	}
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "contact", cascade = CascadeType.ALL, orphanRemoval = true)
	public List<Address> getAddresses() {
		return addresses;
	}

	public void setAddresses(List<Address> addresses) {
		this.addresses = addresses;
	}

}

it is working fine.
but i want annotation put on private field so it is possible ??

@clayreimann
Copy link
Contributor

Why do you want to annotate a private field?

Sent with GitHawk

@aklish
Copy link
Member

aklish commented Aug 27, 2018

This is similar to issue #644.

It is not possible today - but a high priority item. I suspect we'll fix in the next month or so.

@aklish
Copy link
Member

aklish commented Aug 28, 2018

First pass - we'll probably end up following JPA 1.0 conventions where the @Id annotation determines whether the entire entity should use field accessors or property accessors.

We could also search for the @Access annotation and decide on a field by field basis. However, this will be a bit more work - and I'm not convinced it was a useful JPA feature.

This means Elide will only look at fields or only look at properties for any given entity (all determined by where the @Id annotation lives.

Thoughts on this? @wcekan @tbo-axelor @clayreimann @DennisMcWherter

@DennisMcWherter
Copy link
Collaborator

That sounds like a reasonable way to do this to me. It will force some consistency on how your models are annotated which may make them easier to read rather than supporting a mixed case.

That said, is there much extra work to support a mixed approach? The defaulting behavior could be with @Id and we could support @Access.

I like your original suggestion (I personally think it's better style to use one or the other), but you could imagine a situation where the model has been annotated with field-level annotations and then there comes a need for a single getter-based annotation. In this case, the user would have to reannotate the model with getters for this to work rather than just adding an @Access annotation.

Just something to consider. If it's not significantly more work, it may be nicer to consider how code may evolve.

@aklish
Copy link
Member

aklish commented Aug 28, 2018

If we support a mix of properties and fields - we'll need to disambiguate conflicts (where the same attribute is both a property and field). That feels like a refactor of the EntityBinding to me. I'm not opposed to it being done if someone feels compelled. However, the critical part of this to me seems like the need to support field level access (as this is a very common pattern). My guess is that mixed properties and fields is not a common case.

@DennisMcWherter
Copy link
Collaborator

I am certainly not against enabling wholesale one or the other as a first pass. Maybe we proceed with this solution (since it should be fairly straightforward) and can prioritize the work for mixed tasks if we find other users needing it.

aklish added a commit that referenced this issue Aug 29, 2018
@aklish aklish mentioned this issue Aug 29, 2018
@ghost
Copy link
Author

ghost commented Aug 31, 2018

thank u @aklish and @DennisMcWherter

fix in 4.2.7 right.
so when you release 4.2.7 ??

@aklish
Copy link
Member

aklish commented Aug 31, 2018

Probably as soon as #690 #695 and #697 are merged to master.

@ghost
Copy link
Author

ghost commented Sep 7, 2018

hii @aklish ,
what about #691 issue
OneToMany or ManyToOne annotation also put on private field??

@aklish
Copy link
Member

aklish commented Sep 7, 2018

@tbo-axelor It should work. I should probably add some tests for this.

clayreimann pushed a commit that referenced this issue Sep 11, 2018
For entities which annotate the primary key getter (JPA property) with `@Id`, the behavior should be identical to 4.2.6 and earlier.

For entities which annotate the primary key field (JPA field) with `@Id`, Elide will only expose fields and computed attributes/relationships (which can be properties).

Fixes #691 and Fixes #644 and Fixes #443
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants