Hibernate 5.2.1笔记

1、Junit中获取SessionFactory方式

private SessionFactory sessionFactory;
    
    @Override
    protected void setUp() throws Exception {
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure()
                .build();
        try {
            //自动生成数据库表
            //new SchemaExport().create(EnumSet.of(TargetType.DATABASE),new MetadataSources(registry).buildMetadata());
            sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
        }
        catch (Exception e) {
            StandardServiceRegistryBuilder.destroy( registry );
        }
    }

2、ID生成策略

AUTO、IDENTITY、SEQUENCE、TABLE

   @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }

数据库是SQL Server 2008,再生成主键时使用  @GeneratedValue 或者 @GeneratedValue(strategy = GenerationType.AUTO)

数据库生成的策略使用的是SEQUENCE,其他数据库没做测试

3、使用@ManyToMany多对多双向关联时,主键生成策略不起作用的问题依旧

 
原文地址:https://www.cnblogs.com/helloquan/p/5705904.html