Java 使用 Hibernate 的代码优先方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19531450/
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
Code First Apprach with Hibernate
提问by greyfox
I am a PHP Developer in the process of learning Java / Spring MVC / Hibernate. I was wondering if Hibernate supported Code First Approach like Entity is ASP.NET or Doctrine with PHP. All the resources I have seen so far the database if being created first and then you write your classes and XML files. Is it possible to do code first with Hibernate and it generates the database and handles the schema changes? If so can someone link me to a resource explaining how this is done? Thanks in advance.
我是一名 PHP 开发人员,正在学习 Java / Spring MVC / Hibernate。我想知道 Hibernate 是否支持代码优先方法,如 Entity 是 ASP.NET 或 Doctrine with PHP。到目前为止,我所看到的所有资源都是先创建数据库,然后再编写类和 XML 文件。是否可以先使用 Hibernate 编写代码并生成数据库并处理架构更改?如果是这样,有人可以将我链接到解释这是如何完成的资源吗?提前致谢。
While I'm thinking about it can Hibernate generate the code based on an existing schema too?
当我在考虑 Hibernate 也可以基于现有模式生成代码吗?
采纳答案by chrylis -cautiouslyoptimistic-
You're looking at very old resources. What you want to use instead is JPA 2; it's very strongly based on Hibernate, and Hibernate can run as a JPA provider. With JPA, which is similar to Entity Framework, you create POJOs and annotate their fields to provide directives to the JPA provider about how to handle persistence.
您正在查看非常古老的资源。您要使用的是 JPA 2;它非常强大地基于 Hibernate,并且 Hibernate 可以作为 JPA 提供程序运行。使用类似于实体框架的 JPA,您可以创建 POJO 并注释它们的字段,以向 JPA 提供者提供有关如何处理持久性的指令。
There's a decent tutorial here, and I encourage you to look at Spring Roowith its aspect-based approach which frees you from having to manage lots of the bookkeeping by hand.
这里有一个不错的教程,我鼓励您使用基于方面的方法来查看Spring Roo,它使您不必手动管理大量簿记。