Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code First Apprach with Hibernate

Tags:

java

hibernate

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.

While I'm thinking about it can Hibernate generate the code based on an existing schema too?

like image 237
greyfox Avatar asked Oct 23 '13 01:10

greyfox


People also ask

What is a code first approach?

In the ASP.NET MVC framework, the code-first approach is a development model where you first write the code that creates the data access layer, then you write the code that creates the controllers and views. In the code-first approach, you create a model, which is a class that represents the data in the application.

How do you implement code first approach?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…

Why We Use Code First approach in Entity Framework?

Code-First is mainly useful in Domain Driven Design. In the Code-First approach, you focus on the domain of your application and start creating classes for your domain entity rather than design your database first and then create the classes which match your database design.


1 Answers

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.

There's a decent tutorial here, and I encourage you to look at Spring Roo with its aspect-based approach which frees you from having to manage lots of the bookkeeping by hand.

like image 136
chrylis -cautiouslyoptimistic- Avatar answered Sep 20 '22 12:09

chrylis -cautiouslyoptimistic-