java 从实体创建数据库表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26725011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create database tables from entities
提问by Matt Mcdon
Is in hibernate something like code first approach from Entity Framework? I am trying to prepare database tables from java classes (MySQL). I have already prepared java classes with JPA annotations, but I am not sure what to do now (I am using IntelliJ). What should I have in my main to create thoose tables? Or should I use some tools?
在休眠中是否类似于实体框架中的代码优先方法?我正在尝试从 Java 类 (MySQL) 准备数据库表。我已经准备好了带有 JPA 注释的 java 类,但我不确定现在该怎么做(我正在使用 IntelliJ)。我的主要内容应该有什么来创建那些表?还是我应该使用一些工具?
One of my classes looks like this:
我的一堂课看起来像这样:
@Entity
public class Item {
@Id
@GeneratedValue
private long id;
@Column
private String text;
@ManyToMany(mappedBy = "items")
private Set<Container> containers = new HashSet<Container>();
public long getId() {
return id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Set<Container> getContainers() { return containers; }
}
采纳答案by Matt Mcdon
For clarification: I was using JPA2, so configuration was in persistence.xml. What worked for me was setting:
澄清一下:我使用的是 JPA2,所以配置在persistence.xml 中。对我有用的是设置:
<property name="hibernate.hbm2ddl.auto">create</property>
without "hibernate."it was not working.
没有“休眠”。它不起作用。
Also to start it I needed to prepare main function with creating EntityManager, because mapping to db happens when it is created:
同样要启动它,我需要通过创建 EntityManager 来准备主函数,因为在创建时会映射到 db:
public static void main(String [] args){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("NewPersistenceUnit"); //name of persistence unit here
EntityManager entityManager = factory.createEntityManager();
}
回答by siva636
You need to set the hibernate.hbm2ddl.auto
property with value create
. If so, when you run the applications first time, the tables will be created.
您需要hibernate.hbm2ddl.auto
使用 value设置属性create
。如果是这样,当您第一次运行应用程序时,将创建表。
回答by Chaitanya
In hibernate.cfg.xml configuration file you can set a property called hbm2ddl.auto
that tells hibernate to create a table for you:
在 hibernate.cfg.xml 配置文件中,您可以设置一个名为的属性hbm2ddl.auto
,它告诉 hibernate 为您创建一个表:
<property name="hbm2ddl.auto">create</property>
When you run your application for the first time then you can set the property to create
and later you can change it to update
.
当您第一次运行应用程序时,您可以将该属性设置为create
,稍后您可以将其更改为update
。
Follow this link for more details:
请点击此链接了解更多详情:
回答by Thamil
@OneToMany(mappedBy = "")
@Fetch(value = FetchMode.SUBSELECT)
@OneToMany(mappedBy = "")
@Fetch(value = FetchMode.SUBSELECT)
In my case, I have mentioned the same OneToMany
relationship two times in a master table. Because I have two relationships. I have used @Fetch(value = FetchMode.SUBSELECT)
in two relationships. This is working for me fine.
就我而言,我OneToMany
在主表中两次提到了相同的关系。因为我有两种关系。我@Fetch(value = FetchMode.SUBSELECT)
在两个关系中使用过。这对我来说很好。