为了账号安全,请及时绑定邮箱和手机立即绑定

休眠:基于实体类自动创建/更新数据库表

休眠:基于实体类自动创建/更新数据库表

忽然笑 2019-10-08 15:02:41
我有以下实体类(在Groovy中):import javax.persistence.Entityimport javax.persistence.Idimport javax.persistence.GeneratedValueimport javax.persistence.GenerationType@Entitypublic class ServerNode {  @Id  @GeneratedValue(strategy = GenerationType.AUTO)  Long id  String firstName  String lastName}和我的persistence.xml:<?xml version="1.0" encoding="UTF-8"?><persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">    <persistence-unit name="NewPersistenceUnit">        <provider>org.hibernate.ejb.HibernatePersistence</provider>        <properties>            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/Icarus"/>            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>            <property name="hibernate.connection.username" value="root"/>            <property name="hibernate.connection.password" value=""/>            <property name="hibernate.archive.autodetection" value="class"/>            <property name="hibernate.show_sql" value="true"/>            <property name="hibernate.format_sql" value="true"/>            <property name="hbm2ddl.auto" value="create"/>        </properties>        <class>net.interaxia.icarus.data.models.ServerNode</class>    </persistence-unit></persistence>和脚本:import javax.persistence.EntityManagerimport javax.persistence.EntityManagerFactoryimport javax.persistence.Persistenceimport net.interaxia.icarus.data.models.ServerNodedef factory = Persistence.createEntityManagerFactory("NewPersistenceUnit")def manager = factory.createEntityManager()manager.getTransaction().begin()manager.persist new ServerNode(firstName: "Test", lastName: "Server")manager.getTransaction().commit()数据库Icarus存在,但当前没有表。我希望Hibernate基于实体类自动创建和/或更新表。我将如何完成?
查看完整描述

4 回答

  • 4 回答
  • 0 关注
  • 577 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信