Does Hibernate create tables in the database automatically

后端 未结 8 1167
庸人自扰
庸人自扰 2020-12-01 07:23

Upon reading this (archived) tutorial, they have not mentioned anything over creating tables in the DB. Does the Hibernate handle it automatically by creating tables and fie

相关标签:
8条回答
  • 2020-12-01 08:07

    add following property in your hibernate.cfg.xml file

    <property name="hibernate.hbm2ddl.auto">update</property>

    BTW, in your Entity class, you must define your @Id filed like this:

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    @Column(name = "id")
    private long id;
    

    if you use the following definition, it maybe not work:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;
    
    0 讨论(0)
  • 2020-12-01 08:15

    Hibernate can create a table, hibernate sequence and tables used for many-to-many mapping on your behalf but you have to explicitly configure it by calling setProperty("hibernate.hbm2ddl.auto", "create") of the Configuration object. By Default, it just validates the schema with DB and fails if anything not already exists by giving error "ORA-00942: table or view does not exist".

    If you do above configuration then order of performed actions will be:- a) Drop all tables and sequence and do not give an error if they are not already present. b) create all table and sequence c) alter tables with constraints d) insert data into it.

    0 讨论(0)
提交回复
热议问题