`

jpa 总结

    博客分类:
  • Java
阅读更多
@Entity
Specifies that the class is an entity. This annotation is applied to the entity class.
----------------------------------------------------------------------
@Table
This annotation specifies the primary table for the annotated entity. Additional tables may be specified using SecondaryTable  or SecondaryTables annotation.
If no Table annotation is specified for an entity class, the default values apply.

java.lang.String catalog
          (Optional) The catalog of the table.
java.lang.String name
          (Optional) The name of the table.
java.lang.String schema
          (Optional) The schema of the table.
UniqueConstraint[] uniqueConstraints
          (Optional) Unique constraints that are to be placed on the table

----------------------------------------------------------------------
@ManyToOne
This annotation defines a single-valued association to another entity class that has many-to-one multiplicity. It is not normally necessary to specify the target entity explicitly since it can usually be inferred from the type of the object being referenced.

targetEntity
public abstract java.lang.Class targetEntity
    (Optional) The entity class that is the target of the association.
    Defaults to the type of the field or property that stores the association.
    Default:
        void.class

cascade
public abstract CascadeType[] cascade
    (Optional) The operations that must be cascaded to the target of the association.
    By default no operations are cascaded.
    Default:
        {}

fetch
public abstract FetchType fetch
    (Optional) Whether the association should be lazily loaded or must be eagerly fetched. The EAGER strategy is a requirement on the persistence provider runtime that the associated entity must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime.
    Default:
        javax.persistence.FetchType.EAGER

optional
public abstract boolean optional
    (Optional) Whether the association is optional. If set to false then a non-null relationship must always exist.
    Default:
        true

----------------------------------------------------------------------

@OneToMany
Defines a many-valued association with one-to-many multiplicity.

If the collection is defined using generics to specify the element type, the associated target entity type need not be specified; otherwise the target entity class must be specified.

    Example 1: One-to-Many association using generics

    In Customer class:
    @OneToMany(cascade=ALL, mappedBy="customer")
    public Set getOrders() { return orders; }

    In Order class:
    @ManyToOne
    @JoinColumn(name="CUST_ID", nullable=false)
    public Customer getCustomer() { return customer; }

    Example 2: One-to-Many association without using generics

    In Customer class:
    @OneToMany(targetEntity=com.acme.Order.class, cascade=ALL,
            mappedBy="customer")
    public Set getOrders() { return orders; }

    In Order class:
    @ManyToOne
    @JoinColumn(name="CUST_ID", nullable=false)
    public Customer getCustomer() { return customer; }


targetEntity
public abstract java.lang.Class targetEntity
    (Optional) The entity class that is the target of the association. Optional only if the collection property is defined using Java generics. Must be specified otherwise.
    Defaults to the parameterized type of the collection when defined using generics.
    Default:
        void.class

cascade
public abstract CascadeType[] cascade
    (Optional) The operations that must be cascaded to the target of the association.
    Defaults to no operations being cascaded.
    Default:
        {}

fetch
public abstract FetchType fetch
    (Optional) Whether the association should be lazily loaded or must be eagerly fetched. The EAGER strategy is a requirement on the persistenceprovider runtime that the associatedentities must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime.
    Default:
        javax.persistence.FetchType.LAZY

mappedBy
public abstract java.lang.String mappedBy
    The field that owns the relationship. Required unless the relationship is unidirectional.
    Default:
        ""
----------------------------------------------------------------------


@ManyToMany
Defines a many-valued association with many-to-many multiplicity. If the Collection is defined using generics to specify the element type, the associated target entity class does not need to be specified; otherwise it must be specified.

Every many-to-many association has two sides, the owning side and the non-owning, or inverse, side. The join table is specified on the owning side. If the association is bidirectional, either side may be designated as the owning side.

The same annotation elements for the OneToMany annotation apply to the ManyToMany annotation.

    Example 1:
    In Customer class:
    @ManyToMany
    @JoinTable(name="CUST_PHONES")
    public Set getPhones() { return phones; }
    In PhoneNumber class:
    @ManyToMany(mappedBy="phones")
    public Set getCustomers() { return customers; }

    Example 2:
    In Customer class:
    @ManyToMany(targetEntity=com.acme.PhoneNumber.class)
    public Set getPhones() { return phones; }
    In PhoneNumber class:
    @ManyToMany(targetEntity=com.acme.Customer.class, mappedBy="phones")
    public Set getCustomers() { return customers; }
  
    Example 3:
    In Customer class:
    @ManyToMany
    @JoinTable(name="CUST_PHONE",
        joinColumns=
            @JoinColumn(name="CUST_ID", referencedColumnName="ID"),
        inverseJoinColumns=
            @JoinColumn(name="PHONE_ID", referencedColumnName="ID")
        )
    public Set getPhones() { return phones; }
    In PhoneNumberClass:
    @ManyToMany(mappedBy="phones")
    public Set getCustomers() { return customers; }



targetEntity
public abstract java.lang.Class targetEntity
    (Optional) The entity class that is the target of the association. Optional only if the collection property is defined using Java generics. Must be specified otherwise.
    Defaults to the parameterized type of the collection when defined using generics.
    Default:
        void.class

cascade
public abstract CascadeType[] cascade
    (Optional) The operations that must be cascaded to the target of the association.
    Defaults to no operations being cascaded.
    Default:
        {}

fetch
public abstract FetchType fetch
    (Optional) Whether the association should be lazily loaded or must be eagerly fetched. The EAGER strategy is a requirement on the persistenceprovider runtime that the associatedentities must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime.
    Default:
        javax.persistence.FetchType.LAZY

mappedBy
public abstract java.lang.String mappedBy
    The field that owns the relationship. Required unless the relationship is unidirectional.
    Default:

----------------------------------------------------------------------
@JoinTable
This annotation is used in the mapping of associations. It is specified on the owning side of a many-to-many association, or in a unidirectional one-to-many association.

If the JoinTable annotation is missing, the default values of the annotation elements apply. The name of the join table is assumed to be the table names of the associated primary tables concatenated together (owning side first) using an underscore.

    Example:
    @JoinTable(
    name="CUST_PHONE",
    joinColumns=
        @JoinColumn(name="CUST_ID", referencedColumnName="ID"),
    inverseJoinColumns=
        @JoinColumn(name="PHONE_ID", referencedColumnName="ID")
    )

name
public abstract java.lang.String name
    (Optional) The name of the join table.
    Defaults to the concatenated names of the two associated primary entity tables, separated by an underscore.
    Default:
        ""

catalog
public abstract java.lang.String catalog
    (Optional) The catalog of the table.
    Defaults to the default catalog.
    Default:
        ""

schema
public abstract java.lang.String schema
    (Optional) The schema of the table.
    Defaults to the default schema for user.
    Default:
        ""

joinColumns
public abstract JoinColumn[] joinColumns
    (Optional) The foreign key columns of the join table which reference the primary table of the entity owning the association (i.e. the owning side of the association).
    Uses the same defaults as for JoinColumn.
    Default:
        {}

inverseJoinColumns
public abstract JoinColumn[] inverseJoinColumns
    (Optional) The foreign key columns of the join table which reference the primary table of the entity that does not own the association (i.e. the inverse side of the association).
    Uses the same defaults as for JoinColumn.
    Default:
        {}

uniqueConstraints
public abstract UniqueConstraint[] uniqueConstraints
    (Optional) Unique constraints that are to be placed on the table. These are only used if table generation is in effect.
    Defaults to no additional constraints.
    Default:
        {}

----------------------------------------------------------------------

@JoinColumn
Is used to specify a mapped column for joining an entity association.

   Example:

   @ManyToOne
   @JoinColumn(name="ADDR_ID")
   public Address getAddress() { return address; }


name
public abstract java.lang.String name
    (Optional) The name of the foreign key column. The table in which it is found depends upon the context. If the join is for a OneToOne or Many- ToOne mapping, the foreign key column is in the table of the source entity. If the join is for a ManyToMany, the foreign key is in a join table. Default (only applies if a single join column is used): The concatenation of the following: the name of the referencing relationship property or field of the referencing entity; "_"; the name of the referenced primary key column. If there is no such referencing relationship property or field in the entity, the join column name is formed as the concatenation of the following: the name of the entity; "_"; the name of the referenced primary key column.
    Default:
        ""

referencedColumnName
public abstract java.lang.String referencedColumnName
    (Optional) The name of the column referenced by this foreign key column. When used with relationship mappings, the referenced column is in the table of the target entity. When used inside a JoinTable annotation, the referenced key column is in the entity table of the owning entity, or inverse entity if the join is part of the inverse join definition. Default (only applies if single join column is being used): The same name as the primary key column of the referenced table.
    Default:
        ""

unique
public abstract boolean unique
    (Optional) Whether the property is a unique key. This is a shortcut for the UniqueConstraint annotation at the table level and is useful for when the unique key constraint is only a single field. It is not necessary to explicitly specify this for a join column that corresponds to a primary key that is part of a foreign key.
    Default:
        false

nullable
public abstract boolean nullable
    (Optional) Whether the foreign key column is nullable.
    Default:
        true

insertable
public abstract boolean insertable
    (Optional) Whether the column is included in SQL INSERT statements generated by the persistence provider.
    Default:
        true

updatable
public abstract boolean updatable
    (Optional) Whether the column is included in SQL UPDATE statements generated by the persistence provider.
    Default:
        true

columnDefinition
public abstract java.lang.String columnDefinition
    (Optional) The SQL fragment that is used when generating the DDL for the column.
    Defaults to the generated SQL for the column.
    Default:
        ""

table
public abstract java.lang.String table
    (Optional) The name of the table that contains the column. If a table is not specified, the column is assumed to be in the primary table of the applicable entity.
    Default:
        ""

分享到:
评论

相关推荐

    JPA总结各种情况

    JPA详细使用说明!由他一个足以!啥po的继承,一对多,多对多和JPA的配置该文档都有的!

    全面阐释和精彩总结JPA

    ### 全面阐释和精彩总结JPA:Java Persistence API的核心理念与实践 #### JPA:Java持久化的统一标准 JPA(Java Persistence API),作为Sun官方提出的Java持久化规范,其核心目标在于简化并标准化Java应用程序中...

    JPA核心知识总结

    **JPA核心知识总结** Java Persistence API (JPA) 是Java平台上的一个规范,用于管理和持久化Java对象到关系数据库。它提供了一种面向对象的方式来处理数据,使得开发者可以使用面向对象的编程语言来操作数据库,而...

    JPA一些总结.md

    本文档主要记录了一些我在项目中使用JPA时总结的一些小点,包括:配置、查询(包括动态查询+分页)、传参、基本注解等

    JPA注解总结大全!!!!

    ### JPA注解总结大全 Java Persistence API (JPA) 是一种用于管理关系数据库中的对象/关系映射的标准。本文将详细介绍与 JPA 相关的一些常用注解及其使用方法,帮助开发者更好地理解和掌握 JPA 的核心功能。 #### ...

    01_传智播客JPA详解_全面阐释和精彩总结JPA

    Java Persistence API(JPA)是Java平台上的一个标准,用于管理关系数据库中的数据。...在“01_传智播客JPA详解_全面阐释和精彩总结JPA”这个资源中,你将找到关于JPA的深入讲解和实用技巧,帮助你全面掌握这一技术。

    EJB3+JPA开发总结

    ### EJB3+JPA开发总结 #### 一、EJB3概述 EJB3 (Enterprise JavaBeans 3) 是Java EE平台上的一个重要组件,它主要用于构建企业级应用程序中的业务逻辑层。相比于早期版本,EJB3进行了大量改进,简化了许多开发流程...

    JPA_官方文档_中文版

    JPA的出现是对早期EJB规范失败经验的深刻反思与总结。Sun Microsystems在设计JPA时,广泛借鉴了已有ORM框架的优点,如Hibernate,从而打造了一个既易于使用又具备高度可扩展性的ORM规范。这一举措得到了开发社区的...

    jpa学习总结,pdf类型的,详细介绍了jpa的应用

    ### JPA学习总结知识点 #### 1. 什么是JPA? JPA (Java Persistence API) 是Sun官方提出的一种Java持久化规范,旨在为Java开发者提供一种便捷的方式去管理和操作Java应用中的关系型数据。它的主要目标是简化现有的...

    JPA注解总结

    ### JPA注解详解 Java Persistence API (JPA) 是一种用于管理关系型数据库对象的标准。JPA 使用注解和 XML 来配置实体类与数据库表之间的映射关系。本文将详细解读 JPA 注解的基本用法,并通过 Hibernate 框架作为...

    jpa

    总结而言,这篇博客可能涵盖了JPA的基本概念、源码分析、在Spring和Struts框架中的应用,以及如何利用JPA来提高开发效率和代码可维护性。对于想要深入了解JPA和Java持久化技术的开发者来说,这将是一篇非常有价值的...

    JPA Demo 简单的了解下jpa

    **总结** JPA通过提供ORM能力,使得Java开发者可以使用面向对象的方式处理数据库操作,极大地简化了数据存取层的代码。尽管JPA的API可能对于初学者来说较为复杂,但一旦掌握,就能大幅提升开发效率。通过JPA Demo,...

    JPA学习笔记-EJB-04JPA关联映射总结

    JPA(Java Persistence API)是Java平台上的一个标准,用于管理关系数据库中的数据,简化了对象持久化的流程。在EJB(Enterprise JavaBeans)框架中,JPA被广泛使用,提供了一种面向对象的方式来处理数据库交互。本...

    JPA

    ### 总结 JPA是Java应用中处理数据库操作的强大工具,尤其当它与Spring框架结合时,能够提供一个高度集成和可扩展的数据访问解决方案。理解并熟练掌握JPA的各种关系映射机制对于高效地设计和实现数据密集型应用程序...

    JPA参考文档 JPA参考文档

    #### 七、总结 JPA作为一个强大的ORM框架,不仅极大地简化了Java应用程序中对象与数据库之间的持久化操作,还促进了ORM领域的标准化进程。通过使用JPA,开发者可以更加专注于业务逻辑的实现,而不必过多关注底层的...

    jpa的学习---jpa demo工程

    8. **总结** JPA通过提供统一的API,简化了Java应用程序与数据库之间的交互,提高了开发效率。在实际项目中,结合Spring Boot的自动化配置,可以快速构建出功能完备的数据访问层。通过阅读和理解`jpatest`这个Demo...

    JPA学习笔记-EJB-03JPA主键生成策略总结

    ### JPA主键生成策略详解 #### 一、概述 持久化对象的主键生成机制在JPA(Java Persistence API)中占据着重要的位置。它不仅关乎数据的唯一标识符如何确定,还与数据的存储效率及应用逻辑紧密相关。JPA通过`@...

Global site tag (gtag.js) - Google Analytics