*◇◆●○•⊙◎Θ←↙↓↘→↗↑
•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
•Hibernate配置
<class name="Category" table="CATEGORY">
    <id name="id" column="CATEGORY_ID" type="long">
        <generator class="native"/>
    </id>
    ... </class>
XML的等效  JPA 表示形式
@Entity
@Table(name="CATEGORY")
public class Category
{
    private Long id;
    ...
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "CATEGORY_ID")
    public Long getId()
    { return this.id; }
    private void setId(Long id)
    { this.id = id; }
     ...
}
GenerationType.AUTO 跟 native 是对应的配置, 同时 AUTO 也是默认属性, 所以省略也是可以的
在column 省略的情况下 使用的是 属性的名称(上述情况中, 则column 为 id)
上述的@注解是放在方法名之上的, 则其只对该方法有效, 如果放在域上, 则对所有(get, set)都有效
上述的等效表示:
@Id @GeneratedValue
@Column(name = "CATEGORY_ID")
private Long id;
 
 
•Hibernate exposes database identity to the application in two ways:
     The value of the identifier property of a persistent instance
     The value returned by Session.getIdentifier(Object entity)
•hibernate-mapping
    如果在class中添加了 entity-name 属性, 那么Hibernate使用的将是逻辑名  举例来说 如果有以下配置
        <class name="model.UserPojo" entity-name="UserEntity" table="USER_ENTITY">
    那么, 如下代码是不能工作的(save 部分)
    UserPojo user = new UserPojo();
    session.save(user);
    改成  session.save("UserEntity", user);
•These are the three built-in entity modes in Hibernate
    POJO—A domain model implementation based on POJOs, persistent classes.
    MAP—No Java classes are required; entities are represented in the Java application
        with HashMaps. This mode allows quick prototyping of fully dynamic applications.
    DOM4J—No Java classes are required; entities are represented as XML elements, based on
    the dom4j API. This mode is especially useful for exporting or importing data, or for
    rendering and transforming data through XSLT processing.
•Hibernate映射有3中方式
    1. native Hibernate XMLmetadata (Hibernate 自己的配置文件格式)
    2. JPA/Hibernate annotations (注解)
    3. JPA XML (JPA形式的XML文件)    orm.xml
    其中, JPA XML 文件可以覆盖注解形式的映射, 但是原生hibernate配置文件则不行
    优先考虑 注解形式的映射, 如果注解不能解决, 则可以考虑 XML 配置 文件类型的映射方式
•If a RuntimeExceptionis thrown, the current transaction is rolled back, and the
    exception is yours to handle. If a checked application exception is thrown, Hibernate
    wraps the exception into a RuntimeException.
many-to-many关联
    public class Category { .. private Set items = new HashSet(); ..}
    public class Items{.. private Set categories = new HashSet(); ..}
        这个 双端 的关联方法只要一个就可以了  另一方通过这一方来控制
        即如果已经有了 这个 addCategory方法
        那么 Category 类中就不再需要addItems()方法了
        
public void addCategory(Category category)
{
if (category == null)
    throw new IllegalArgumentException("Null category");
category.getItems().add(this);
categories.add(category);
}
•在Hibernate映射类中, 如果有set之类的集合属性的话 那么添加一个 add()方法是个不错的选择
    在这种情况下 集合的set方法就是可选的了(删除页眉关系)
    如下所示
public void addChildCategory(Category childCategory)
{
if (childCategory == null)
    throw new IllegalArgumentException("Null child category!");
if (childCategory.getParentCategory() != null)
    childCategory.getParentCategory().getChildCategories().remove(childCategory);
childCategory.setParentCategory(this);
childCategories.add(childCategory);
}
•persistence.xml 配置文件
    Every persistence unit needs a name,  一般可以把它设置为工程的名称
•A SessionFactoryrepresents a particular logical data-store configuration in a Hibernate application.
    The EntityManagerFactoryhas the same role in a JPA
application, and you configure an EntityManagerFactory(EMF) either with con-figuration files or in
application code just as you would configure a SessionFac-tory.
• 采用注解的Hibernate 所需要的类包
    hibernate-annotations.jar  与 ejb3-persistence.jar
• 数据表生成的3种方法
    1.Ant脚本的配置
    2.hibernate.hbm2ddl.auto 属性的配置
        update : -> SchemaUpdate / create :-> SchemaExport / validate :-> SchemaValidator  (对应实现类)
    3.编程实现
Configuration cfg = new Configuration().configure();
SchemaExport schemaExport = new SchemaExport(cfg);
schemaExport.create(false, true);
The tool used for schema generation is hbm2ddl; its class is org.hibernate.
tool.hbm2ddl.SchemaExport, so it’s also sometimes called SchemaExport.
 
 
• Hibernate.cfg.xml 要放在src文件夹下 不然会抛出异常(默认的 new Configuration().configure(); 操作)
    不过也可以 通过 new Configuration().configure("/xx/xx.cfg.xml") 来显示指定cfg.xml 配置文件的路径.
•The regular way of initializing Hibernate is to build a SessionFactoryobject from a Configurationobject. 
•Hibernate  数据库的接入
    Session
    Transaction
    Query
•分层体系结构的特定
     Layers communicate from top to bottom. A layer is dependent only on the layer directly below it.
     Each layer is unaware of any other layers except for the layer just below it.     
 
 
 
    •If you wish to represent a many-to-many association in a relational database, you must introduce a new table called a link table.
    •The single most important thing you can do to improve the performance of data access code is to minimize the number of requests to the database.
 
 
■ Storage, organization, and retrieval of structured data
■ Concurrency and data integrity
■ Data sharing
 
数据在一个应用中的主要职责???
 
Granularity refers to the relative size of the types you’re working with. 什么意思?