zoukankan      html  css  js  c++  java
  • Hibernate注解

    2.2. Mapping with JPA

    2.2.1. Marking a POJO as persistent entity实体注解

    @Entity
    public class Flight implements Serializable {
        Long id;
    
        @Id
        public Long getId() { return id; }
    
        public void setId(Long id) { this.id = id; }
    }

    @Entity declares the class as an entity (i.e. a persistent POJO class), @Id declares the identifier property of this entity. The other mapping declarations are implicit. The class Flight is mapped to the Flight table, using the column id as its primary key column.

    @Entity声明一个实体类。

    @Id声明该类的主键

    2.2.1.1. Defining the table

    @Table is set at the class level; it allows you to define the table, catalog, and schema names for your entity mapping. If no @Table is defined the default values are used: the unqualified class name of the entity.

    @Table声明在类级别。用来定义表名,如果不声明,默认类名即为表名。

    @Entity
    @Table(name="tbl_sky")
    public class Sky implements Serializable {
       ...
    }  

    2.2.2. Mapping simple properties通用属性注解

    2.2.2.1. Declaring basic property mappings

    Every non static non transient property (field or method depending on the access type) of an entity is considered persistent, unless you annotate it as @Transient.

    任务非静态非临时变量都会被持久化,除非声明为@Transient

    public transient int counter; //transient property
    
    private String firstname; //persistent property
    
    @Transient
    String getLengthInMeter() { ... } //transient property
    
    String getName() {... } // persistent property
    
    @Basic
    int getLength() { ... } // persistent property
    
    @Basic(fetch = FetchType.LAZY)
    String getDetailedComment() { ... } // persistent property
    
    @Temporal(TemporalType.TIME)
    java.util.Date getDepartureTime() { ... } // persistent property           
    
    @Enumerated(EnumType.STRING)
    Starred getNote() { ... } //enum persisted as String in database
    @Lob
    public String getFullText() {
        return fullText;
    }
    
    @Lob 
    public byte[] getFullCode() {
        return fullCode;
    }

    @Transient, will be ignored by the entity manager。不会被持久化。

    fetch = FetchType.LAZY 延迟加载。The detailedComment property value will be lazily fetched from the database once a lazy property of the entity is accessed for the first time.

    Temporal data can have DATE, TIME, or TIMESTAMP precision (ie the actual date, only the time, or both). Use the @Temporal annotation to fine tune that.

    @Lob indicates that the property should be persisted in a Blob or a Clob depending on the property type: java.sql.Clob, Character[], char[] and java.lang.String will be persisted in a Clob. java.sql.Blob, Byte[], byte[] and serializable type will be persisted in a Blob.

    2.2.3. Mapping identifier properties主键注解

    The @Id annotation lets you define which property is the identifier of your entity. This property can be set by the application itself or be generated by Hibernate (preferred). You can define the identifier generation strategy thanks to the @GeneratedValue annotation.

    @Id定义主键

    @GeneratedValue定义主键生成策略

    2.2.3.1. Generating the identifier property

    JPA defines five types of identifier generation strategies:五种主键生成策略

    • AUTO - either identity column, sequence or table depending on the underlying DB

    • TABLE - table holding the id

    • IDENTITY - identity column

    • SEQUENCE - sequence

    • identity copy - the identity is copied from another entity

    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_STORE")
    public Integer getId() { ... }  
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long getId() { ... }     

    2.2.5. Mapping entity associations/relationships关系映射注解

    官网:http://hibernate.org/

    说明文档:

    英文:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

    中文:http://docs.jboss.org/hibernate/annotations/3.4/reference/zh_cn/html_single/

  • 相关阅读:
    C#文件下载(实现断点续传)
    C#winform实现跑马灯
    Asp.net GridView转换成DataTable
    SQL Server 索引重建脚本
    SQL SERVER数据库维护与重建索引
    python try except 出现异常时,except 中如何返回异常的信息字符串
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte
    bower 安装依赖提示 EINVRES Request to https://bower.herokuapp.com/packages/xxx failed with 502
    EINVRES Request to https://bower.herokuapp.com/packages/ failed with 502
    使用notepad++插件远程编辑linux下的配置文件
  • 原文地址:https://www.cnblogs.com/sunilsun/p/4766008.html
Copyright © 2011-2022 走看看