Mat*_*don 5 java mysql orm hibernate jpa
在休眠中是否类似于实体框架中的代码优先方法?我正在尝试从 java 类(MySQL)准备数据库表。我已经准备好了带有 JPA 注释的 java 类,但我不知道现在该怎么做(我正在使用 IntelliJ)。我的 main 中应该有什么来创建这些表?或者我应该使用一些工具?
我的一门课是这样的:
@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; }
}
Run Code Online (Sandbox Code Playgroud)
澄清一下:我使用的是 JPA2,因此配置位于persistence.xml中。对我有用的是设置:
<property name="hibernate.hbm2ddl.auto">create</property>
Run Code Online (Sandbox Code Playgroud)
没有“冬眠”。它不起作用。
另外,为了启动它,我需要通过创建 EntityManager 来准备主函数,因为映射到数据库是在创建时发生的:
public static void main(String [] args){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("NewPersistenceUnit"); //name of persistence unit here
EntityManager entityManager = factory.createEntityManager();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22236 次 |
| 最近记录: |