• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Netbeans Hibernate

 
Ranch Hand
Posts: 53
1
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Java SE program that uses Postgres SQL and I am developing with Netbeans 17 (the latest). I want to include Hibernate but can't find it in the Netbeans release! Any idea what's happening?
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate has nothing to do with NetBeans. You'll need to add it to your build manually.
 
Bill Babbitt
Ranch Hand
Posts: 53
1
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the Apache NetBeans 17 tutorial titled "Using Hibernate in a Java Swing Application" seems to say Hibernate is part of the NetBeans IDE release.

"Adding Hibernate Support to the Project
To add support for Hibernate to a J2SE project you need to add the Hibernate library to the project. The Hibernate library is included with the IDE and can be added to any project by right-clicking the 'Libraries' node in the Projects window, selecting 'Add Library', and then selecting the Hibernate library in the Add Library dialog box.
The IDE includes wizards to help you create the Hibernate files you may need in your project. You can use the wizards in the IDE to create a Hibernate configuration file and a utility helper class. If you create the Hibernate configuration file using a wizard the IDE automatically adds the Hibernate libraries to the project."

Yet, following these instructions, I get to the "Add Library" point and find no Hibernate selection.

Okay, I have to believe Hibernate IS NOT included in the NetBeans release. I still want to add Hibernate to my project. I use Maven and can find the POM dependencies XML addition for Hibernate. Do I still need to add the jar file to the libraries?

Last, is there a difference between Hibernate and Spring Hibernate?
 
Saloon Keeper
Posts: 27708
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NetBeans is a product of Oracle, assuming that they inherited it from Sun and kept it. I haven't been keeping track.

Hibernate is a product of hibernate.org which more or less became a subsidiary of Red Hat, which is now part of IBM. I may be off on some details, but IBM and Oracle are competitors.

There may be a plug-in for NetBeans that supports Hibernate, but I would not expect to find Hibernate itself bundled with NetBeans. Partly because that's not how one generally builds Java projects these days, partly because of the previously-mentioned vendor conflicts, but also because Hibernate is an extremely active project and you cannot just "throw in" a generic Hibernate because it would be an obsolete version probably in under 6 months.

The Spring Framework is a product of spring.io, which is also separate from IBM and Oracle. It's primarily an open-source Inversion of Control (IoC) framework for application support. It contains quite a few modules which in turn sport many sub-modules. When using Hibernate under Spring, you'd be using the Spring Data module and under it, the Spring JPA (Java Persistence Architecture). JPA supports virtually any ORM provider, of which Hibernate is only one. I've also used it with Apache OpenJPA, for example.

Just to add further complexity. Hibernate comes in 2 flavors: legacy Hibernate and Hibernate JPA. Legacy Hibernate was the original. The Hibernate group met with various other organizations to define JPA, which is a subset of EJB version 3 and part of the JEE standard. They are a lot alike, but the key difference is that Legacy Hibernate employs a "Session", but JPA's equivalent is an EntityManager. I'm not sure what, if any support Spring has for Legacy Hibernate, and I recommend avoiding Legacy Hibernate anyway. JPA is an industry standard, but if Hibernate retires its legacy version that will be the end of it.
 
Bill Babbitt
Ranch Hand
Posts: 53
1
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Lord, Tim. You've made my head hurt. I want the persistence and like the simplicity of SQL interface the Hibernate promises, but from the sound of it, things seem to be spinning in different directions. Maybe just using JPA might be the prudent choice this time. Thanks for the help.  
 
Tim Holloway
Saloon Keeper
Posts: 27708
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not so bad. Spring Data JPA makes Hibernate much more fun to work with. Everything works seamlessly as a unit.
 
Bill Babbitt
Ranch Hand
Posts: 53
1
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I will give the Hibernate a try. I use Maven and found a bunch of artifacts in the https://mvnrepository.com/artifact/org.hibernate location. Again, I'm not sure which Hibernate this is, but I'm pretty sure this isn't Spring Hibernate.

Which dependency(s) XML code should I use in my POM file? I have found several sites and the 2008 book "Harnessing Hibernate" that talk about Maven Hibernate dependencies, but there seems to be no consistency. As I said earlier, I want Persistence and the ORM relationship with my Postgres database. Here is what my POM file looks like now (My IDE is Netbeans):



By the way, the book isn't much help.
 
Tim Holloway
Saloon Keeper
Posts: 27708
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There isn't any such thing as "Spring Hibernate". There's Spring (Spring Core, Spring Data, Spring JPA), and then there's Hibernate. You wire them together, but they are completely separate products.

Your POM is a mess. I'm not sure what those Eclipse dependencies are supposed to be good for. You can't use Hibernate with that POM because it's set up for Apache OpenJPA, which is a completely different JPA than Hibernate JPA. You only get one JPA provider, though it mostly doesn't matter which one you use, since JPA is a standard.

Note also that a properly-designed webapp also would not pull in the PostgreSQL JDBC driver because it should be using a connection pool and the driver would be installed in the webapp server not the webapp.

As to "which version" of Hibernate you get, that's what the "version" element of the dependency selects. Maven doesn't just have one version in its repository, it has probably 2 dozen or more by now, since every time a new release of Hibernate comes out, another version gets put into the Maven repository.
 
Bill Babbitt
Ranch Hand
Posts: 53
1
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Tim. I agree the POM file isn't right. I have no idea where the eclipse dependencies came from. They mysteriously appeared once and I just left them in because the program seemed to compile and run. I intended to take them out on a cleanup.

The ApacheJPA was added because I have been really confused about Hibernate and needed some persistence.

This is not a web app. It will be only as multiple instances on  a local network.
 
Bill Babbitt
Ranch Hand
Posts: 53
1
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help, Tim. This may seem naïve, and perhaps I am somewhat naïve. Originally, I conceived a typical client-server application where copies of my Java client application would utilize a centralized PostgreSQL database on the local network. I have been working on the project using NetBeans and Java SE. Somehow I started down the rabbit hole and thought Hibernate might simplify working with the Database, although so far that has not been a problem. Suddenly the light came on and I realized I was headed toward enterprise tools while using only Java SE. This got me wondering if i SHOULD be using Java EE, JajartaEE, or Spring. I'm not thrilled about the learning curve prospect with these tools. Is there a sane way out of this rabbit hole?
 
Tim Holloway
Saloon Keeper
Posts: 27708
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the quick and dirty solution is simply not to use Java. Although the inside-out nature of JavaScript has soured me on that platform and the lack of a universal database interface like JDBC for Python means that neither of those options is as simple as they should be, either.

Spring is not JEE. It's an independent product, but its Tinker Toy™ approach to wiring components together is a very useful thing and, I'd argue a "must-have" skill for serious professional work. JEE is the essential standard for webapps as well as for services like the Java Persistence Architecture (JPA).

Java EE is simply extra libraries on top of Java SE so you use the same JDK for both platforms.

Hibernate makes database access simpler, but it can be overkill if you only want to do simple things with only a few tables. For the really lightweight stuff, JDBC is all that you need. You can either use brute-force JDBC or Spring Data's Spring JDBC module. As with Spring Data JPA, Spring handles a lot of the grunt work so you have less to code and debug.

I don't have a simple example online at the moment, but if you're looking for how a Spring Hibernate webapp looks, you might want to browse the files here: https://gogs.mousetech.com/mtsinc7/gourmetj-springboot

The app itself is a recipe database system that allows you to enter, import, search and display your favorite recipes and you can see it in action at https://gourmetj.mousetech.com . It's based on Spring Boot, so there's a certain amount of "magic" in there that even I don't fully understand and to make it worse, I'm using a supplement that makes it work with my favorite web GUI (JavaServer Faces, which is part of JEE).

What you might find most interesting is the persistence directory. That's where the Hibernate JPA stuff lives. The DAOs are the classes that access the database tables. The model directory contains the Entity Model (table) definitions. And the service classes are what the rest of the app uses to actually do the database logic, since often you don't work with just one table, but instead have a "working set" of related tables (such as invoice header and detail or recipe and ingredients).

The actual choice of what database type to use as well as its URL and security credentials goes into the Spring Boot configuration file(s). Spring Boot can get configuration data from a variety of sources, but I use the application.yml to define the defaults.

I'll be happy to explain if you have any questions.

If you're interested in JavaServer Faces, it's an MVC-based UI where the Views are defined in template (.xhtml) files, and get their data from backing beans (Models). The backing beans generally also have action methods to allow business logic to be attached to the GUI. Some versions of NetBeans apparently incorrectly called them Controllers, but in JSF, the actual MVC Controllers are all pre-written parts of JSF itself.

This project was not built with NetBeans, but you can use Maven to create the WAR. How difficult it would be to import into NetBeans I cannot say, but since it's in standard Maven project structure, shouldn't be too hard.
 
Bill Babbitt
Ranch Hand
Posts: 53
1
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, Tim. This is all good information, but one question remains: Can I use JPA with Java SE?
 
Tim Holloway
Saloon Keeper
Posts: 27708
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, literally speaking, no, because JPA is part of JEE. But the point is, JEE is just a set of libraries to support a particular mode of running a Java app. As I said, the exact same JVM and JDK is used for vanilla SE and EE Java apps. It's just that an EE app, such as Tomcat, has EE JARs in its classpath. If you take a "Java SE" app and add JPA to it, it's technically become a JEE app, but you don't change anything about the JDK or JVM to do that.

JPA can be used from a simple command-line app or from a web application and I've done both. In the full-stack JEE webapp servers like WebSphere, WehLogic and Firefly, JPA is built into the server itself, as is EJB, if which JPA is a subset. For limited servers like Tomcat and jetty, you simply plunk in some JPA implementation JARs into the WAR you build to make up for the fact that those JARs aren't built into the server.
 
Bill Babbitt
Ranch Hand
Posts: 53
1
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Tim. you've been a big help.
reply
    Bookmark Topic Watch Topic
  • New Topic