梁山县网站建设,汉字域名的网站,做网站的傻瓜软件,硬件开发设计在上一篇文章中#xff0c;我展示了两种读取/写入持久实体状态的不同方法-字段和属性。 使用字段访问模式时#xff0c;JPA使用反射直接从实体的字段读取状态值。 如果我们没有明确指定列名#xff0c;它将直接将字段名转换为数据库列名。 在属性访问模式下#xff0c;使用… 在上一篇文章中我展示了两种读取/写入持久实体状态的不同方法-字段和属性。 使用字段访问模式时JPA使用反射直接从实体的字段读取状态值。 如果我们没有明确指定列名它将直接将字段名转换为数据库列名。 在属性访问模式下使用getter / setter方法读取/写入状态值。 在这种情况下我们使用相同的注释来注释实体状态的getter方法而不是字段。 如果我们未明确指定数据库列名称则按照JavaBean约定确定它们即通过从getter方法名称中删除“ get”部分并将该方法名称其余部分的首字母转换为小写字符来确定。 我们可以通过在实体类声明中使用Access注释来指定用于实体的访问方式。 此注释采用AccessType类型的参数在javax.persistence包中定义枚举该参数具有对应于两种不同访问模式 FIELD和PROPERTY 的两个不同值。 例如我们可以通过以下方式为Address实体指定属性访问模式 Entity
Table(name tbl_address)
Access(AccessType.PROPERTY)
public class Address {private Integer id;private String street;private String city;private String province;private String country;private String postcode;private String transientColumn;IdGeneratedValueColumn(name address_id)public Integer getId() {return id;}public Address setId(Integer id) {this.id id;return this;}public String getStreet() {return street;}public Address setStreet(String street) {this.street street;return this;}public String getCity() {return city;}public Address setCity(String city) {this.city city;return this;}public String getProvince() {return province;}public Address setProvince(String province) {this.province province;return this;}public String getCountry() {return country;}public Address setCountry(String country) {this.country country;return this;}public String getPostcode() {return postcode;}public Address setPostcode(String postcode) {this.postcode postcode;return this;}
} 关于上述示例的几点注意事项 如前所述我们现在使用 Id GeneratedValue和Column批注注释实体ID的getter方法。 由于现在列名称将通过解析getter方法来确定因此我们不再需要使用Transient注释来标记transientColumn字段。 但是如果Address实体具有名称以“ get”开头的任何其他方法则我们需要在其上应用Transient 。 如果一个实体没有显式的访问模式信息就像我们在本系列第一部分中创建的Address实体一样那么JPA会采用默认的访问模式。 这个假设不是随机的。 相反JPA首先尝试找出Id批注的位置。 如果在字段上使用Id批注则假定为字段访问模式。 如果在getter方法上使用Id批注则假定为属性访问模式。 因此即使我们在上面的示例中从Address实体中删除了Access批注映射仍将有效并且JPA将采用属性访问模式 Entity
Table(name tbl_address)
public class Address {private Integer id;private String street;private String city;private String province;private String country;private String postcode;private String transientColumn;IdGeneratedValueColumn(name address_id)public Integer getId() {return id;}// Rest of the class........ 有关访问模式需要记住的一些重要要点 如果使用字段访问模式则永远不要将字段声明为公共字段。 实体的所有字段都应具有专用 最佳 受保护或默认访问类型。 其背后的原因是将字段声明为public将允许任何不受保护的类直接访问实体状态这很容易破坏提供程序的实现。 例如假设您有一个实体其所有字段都是公共的。 现在如果此实体是一个受管实体这意味着它已经保存到数据库中并且任何其他类都更改了其id的值然后尝试将所做的更改保存回数据库则可能会遇到不可预知的行为在以后的文章中将尝试详细阐述该主题。 甚至实体类本身也只能在初始化期间直接操作字段即在构造函数内部。 在属性访问模式下如果我们将注释应用于setter方法而不是getter方法则将忽略它们。 也可以将这两种访问类型混合使用。 假设您要对实体的除一种状态外的所有状态都使用字段访问模式而对于其余一种状态则要使用属性访问模式因为您想在向/从状态值读取/写入状态值之前执行某种转换。数据库。 您可以按照以下步骤轻松完成此操作 用Access批注标记实体并指定AccessType.FIELD作为所有字段的访问方式。 使用Transient批注标记您不喜欢使用字段访问模式的字段。 使用Access批注标记属性的getter方法并指定AccessType.PROPERTY作为访问方式。 下面的示例演示了这种方法因为邮政编码已更改为使用属性访问模式 Entity
Table(name tbl_address)
Access(AccessType.FIELD)
public class Address {IdGeneratedValueColumn(name address_id)private Integer id;private String street;private String city;private String province;private String country;/*** postcode is now marked as Transient*/Transientprivate String postcode;Transientprivate String transientColumn;public Integer getId() {return id;}public Address setId(Integer id) {this.id id;return this;}public String getStreet() {return street;}public Address setStreet(String street) {this.street street;return this;}public String getCity() {return city;}public Address setCity(String city) {this.city city;return this;}public String getProvince() {return province;}public Address setProvince(String province) {this.province province;return this;}public String getCountry() {return country;}public Address setCountry(String country) {this.country country;return this;}/*** We are now using property access mode for reading/writing* postcode*/Access(AccessType.PROPERTY)public String getPostcode() {return postcode;}public Address setPostcode(String postcode) {this.postcode postcode;return this;}
} 这里要注意的重要一点是如果我们不使用Access注释对类进行注释以将字段访问模式显式指定为默认模式而是对字段和getter方法进行注释则映射的结果行为将是不确定的。 这意味着结果将完全取决于持久性提供程序即一个提供程序可能选择默认使用字段访问模式一个可能使用属性访问模式或者一个可能决定引发异常 今天就这样。 如果您发现任何问题/有任何疑问请随时发表评论 直到下一次。 翻译自: https://www.javacodegeeks.com/2014/10/jpa-tutorial-mapping-entities-part-3.html