Java JPA implementation – how to read / set properties?

I'm reading the book beginning Java ee6 platform and GlassFish 3. I have some trouble understanding the access types of fields / attributes What's the difference between them?

How does it read / set properties through the JPA implementation (in this case, eclipse link)? For example, if it is attribute access, you can read / set values through possible verification. These values can be placed in the get / set method, while the field access option does not set / obtain values through these methods, but directly on the field? Is the type set by where I put the @ ID annotation?

Solution

@The access annotation type indicates how JPA should set or get fields in an object An accesstype Field JPA will set the field directly using reflection and will not use any of the provided setter methods It is useful if your class keeps track of "dirty" fields through setter methods Conversely, setting @ access (value = accesstype. Property) instructs JPA to use setter and getter methods when accessing fields

You can add logging to the setter method or system out. Printlns then changes the @ access annotation to prove this For example:

@Id
@Access(value=AccessType.PROPERTY)
private Long Id;
public void setId(Long id) { System.out.println("SET"); this.Id = id; }

Set and this will be printed:

@Id
@Access(value=AccessType.FIELD)
private Long Id;
public void setId(Long id) { System.out.println("SET"); this.Id = id; }

Will not!

It doesn't matter where you put the comments, at least in Hibernate; -)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>