Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA 2.0字段注释与方法注释_Java_Database_Jpa - Fatal编程技术网

Java JPA 2.0字段注释与方法注释

Java JPA 2.0字段注释与方法注释,java,database,jpa,Java,Database,Jpa,在JPA2.0中,注释字段和注释方法(通常是getter)之间的区别是什么 字段注释示例 @Entity public class MainEntity { @Id private Long id @OneToMany private RelatedEntity relatedEntity //getters and setters and possible other methods ... } @Entity public class

在JPA2.0中,注释字段和注释方法(通常是getter)之间的区别是什么

字段注释示例

@Entity
public class MainEntity {

    @Id
    private Long id

    @OneToMany
    private RelatedEntity relatedEntity

    //getters and setters and possible other methods
    ...

}
@Entity
public class MainEntity {

    @Id
    private Long id;

    private RelatedEntity relatedEntity

    //getters and setters and possible other methods
    @OneToMany
    public RelatedEntity getRelatedEntity(){
           return relatedEntity
    }

    //other methods etc
    ...

 }
带有方法注释的示例

@Entity
public class MainEntity {

    @Id
    private Long id

    @OneToMany
    private RelatedEntity relatedEntity

    //getters and setters and possible other methods
    ...

}
@Entity
public class MainEntity {

    @Id
    private Long id;

    private RelatedEntity relatedEntity

    //getters and setters and possible other methods
    @OneToMany
    public RelatedEntity getRelatedEntity(){
           return relatedEntity
    }

    //other methods etc
    ...

 }

从用户的角度来看,在一致性之前并没有区别,但在不同的地方使用注释会改变JPA提供者的行为(hibernate、EclipseLink等)

设置注释的位置为JPA提供者提供了有关您要使用的内容的信息。若在这两个位置混合使用该设置注释,则提供程序将选择一个注释并忽略其余注释。在第二个列表中的示例中,hibernate将忽略
@Id
,因为方法上有
@OneToMany
,这意味着您更喜欢使用
AccessType.PROPERTY

当然,有时我们不想使用属性访问,因为我们有一些额外的方法,可以提供一些逻辑并与命名约定相匹配。然后我们应该使用
AccessType.FIELD


在项目中,您应该使用一致的样式。混合样式是有效的,但您需要为POJO中的几乎所有元素定义
@Access

从用户的角度来看,在一致之前没有区别,但是在不同的位置使用注释会改变JPA提供者的行为(hibernate、EclipseLink等)

设置注释的位置为JPA提供者提供了有关您要使用的内容的信息。若在这两个位置混合使用该设置注释,则提供程序将选择一个注释并忽略其余注释。在第二个列表中的示例中,hibernate将忽略
@Id
,因为方法上有
@OneToMany
,这意味着您更喜欢使用
AccessType.PROPERTY

当然,有时我们不想使用属性访问,因为我们有一些额外的方法,可以提供一些逻辑并与命名约定相匹配。然后我们应该使用
AccessType.FIELD


在项目中,您应该使用一致的样式。混合样式是有效的,但您需要为POJO中的几乎所有元素定义
@Access

使用
JPA
您可以使用这两种方法来映射实体类中表的列;从模式生成的角度来看,字段/方法访问不会改变任何东西,也不会改变已翻译的查询。一般来说,字段注释更干净(Spring之类的框架鼓励这样做),方法注释可以赋予您更大的灵活性(比如从抽象实体类继承)

请注意,在第二个示例中有一个错误:

@Entity
public class MainEntity {

    private Long id;

    private RelatedEntity relatedEntity

    //getters and setters and possible other methods
    @Id
    public Long getId() {
        return id;
    }

    @OneToMany
    public RelatedEntity getRelatedEntity(){
           return relatedEntity
    }

    //other methods etc
    ...
 }

使用
JPA
可以使用这两种方法在实体类中映射表的列;从模式生成的角度来看,字段/方法访问不会改变任何东西,也不会改变已翻译的查询。一般来说,字段注释更干净(Spring之类的框架鼓励这样做),方法注释可以赋予您更大的灵活性(比如从抽象实体类继承)

请注意,在第二个示例中有一个错误:

@Entity
public class MainEntity {

    private Long id;

    private RelatedEntity relatedEntity

    //getters and setters and possible other methods
    @Id
    public Long getId() {
        return id;
    }

    @OneToMany
    public RelatedEntity getRelatedEntity(){
           return relatedEntity
    }

    //other methods etc
    ...
 }

有什么区别?一个是对字段进行注释,另一个是对方法进行注释!对于持久性,没有区别,因为JPA提供者支持这两种方式。除此之外,它完全是基于观点的。有什么区别?一个是对字段进行注释,另一个是对方法进行注释!对于持久性,没有区别,因为JPA提供者支持这两种方式。除此之外,它完全是基于意见的