Hibernate or JPA or JDBC or?

Hibernate or JPA or JDBC or?

4 down vote favorite

1

share [g+] share [fb]

I am developing a Java Desktop Application but have some confusions in choosing a technology for my persistence layer.

Till now, I have been using JDBC for DB operations. Now, Recently I learnt Hibernate and JPA but still I am a novice on these technologies.

Now my question is What to use for my Java Desktop Application from the following?

  • JPA

  • Hibernate

  • JDBC

  • DAO

  • any other suggestion from you...

I know that there is no best choice from them and it totally depends on the complexity and the requeirements of the project so below are the requirements of my project

  1. It's not a complex application. It contains only 5 tables (and 5 entities)
  2. I wan't to make my code flexible so that I can change the database later easily
  3. The size of the application should remain as small as possible as I will have to distribute it to my clients through internet.
  4. It must be free to use in commercial development and distribution.

==================================== EDITED =======================================

On the basis of the below answers, I would like to go with JPA so as to prevent myself from writing vendor-specific SQL code.

But I have some problems in JPA which are mentioned at http://stackoverflow.com/questions/2560125/java-persistence-api

 

 

Here's my take:

  • JPA: Agnostic way to do Java persistence without coupling your clients to Hibernate, TopLink, etc.
  • Hibernate: Good choice if you have an object model to map to.
  • JDBC: All Java persistence is built on this. Lowest level
  • DAO: More of a pattern than a technology; CRUD operation interface.
  • iBatis: Halfway between JDBC (raw SQL) and Hibernate (ORM).
  • JDO: Java Data Objects, which is another specification for Java persistence. (e.g., Apache JDO)

It's not a complex application. It contains only 5 tables (and 5 entities)

Any of these will work, but JDBC will be the simplest. All the others are built on top of JDBC.

I want to make my code flexible so that I can change the database later easily

Schema changes will have similar effects in all technologies.

The size of the application should remain as small as possible as I will have to distribute it to my clients through internet.

Using JPA or Hibernate will require JARs that will add to the size of your deployment. JDBC will minimize this.

It must be free to use in commercial development and distribution.

See licenses of all technologies. Shouldn't be a problem with any of them.

FYI: It's possible to write a generic DAO interface:

package persistence;

import java.io.Serializable;
import java.util.List;

public interface GenericDao<T, K extends Serializable>
{
    T find(K id);
    List<T> find();
    List<T> find(T example);
    List<T> find(String queryName, String [] paramNames, Object [] bindValues);

    K save(T instance);
    void update(T instance);
    void delete(T instance);
}

If your objects map 1:1 with your five tables, I'd say that JPA is overkill squared.

Is your app currently on the order of 3MB JAR? If no, then Hibernate or JPA will more than double the size. You can quantify exactly how much. And there's more than one JAR, because they both have dependencies.

YAGNI says that you should keep it simple. It's five tables!

Changing vendor, if you do it properly, means switching a JDBC driver JAR, changing the driver class name, and adding the new connection URL - which you have to do no matter what technology you pick.

I find that databases don't change that radically. You'll change the schema, but the entire vendor? Not likely, especially if you have several clients. It'll be a major inconvenience to make a user base switch databases.

Which one were you planning to ship with? HSQL or something that will require an installation like MySQL? That's a more pertinent concern.

 

 

 

JPA is certainly to way to go is you want to use object relation mapping - it's implementation agnostic(meaning you can use it with Hibernate, Toplink, etc) and it's the de facto standard. Hibernate has a richer feature set, but this is a non-standard solution - many people use it though... I personally always use JPA backed by Hibernate. I try to stay away from the hibernate specific stuff, but if need it - it's there for me.

Using a framework such as JPA/Hibernate for 5 tables can be a bit of a overkill though. Your application will be around 5MB bigger and will consume a litter more memory. You may simply opt to use JDBC paired with DAO.

Since JDBC uses SQL which is vendor(database) specific this might be problematic is you're planning on using different databases. JPA has a clear advantage in this area.

Whatever you choose you cannot go wrong - you need to ask yourself mostly is the increased size and memory consumption an issue and are you willing to write boilerplate DAO JDBC code. I generally hate this :-)

 

 

 

 

### Spring JPAHibernate集成概述 Spring Data JPA 使用 JPA 将数据存储在关系数据库中,而 HibernateJPA 的一种实现方式[^1]。这种组合允许开发者利用标准的 JPA API 进行持久化操作的同时享受 Hibernate 提供的强大特性。 ### 配置依赖项 为了使项目支持 Spring Data JPAHibernate,在 `pom.xml` 文件中需引入必要的 Maven 依赖: ```xml <dependencies> <!-- Spring Boot Starter Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- MySQL Connector (或其他数据库驱动) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- Other dependencies... --> </dependencies> ``` ### 创建实体类 定义一个简单的 Java 实体类表示表结构,并标注相应的 JPA 注解: ```java @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false, length = 50) private String name; // Getters and Setters... } ``` ### 定义仓库接口 创建自定义的 Repository 接口扩展 `JpaRepository<User, Long>` 来获取预设的方法以及编写特定业务逻辑所需的查询语句[^5]: ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { List<User> findUserByNameLike(String name); User findById(Long id); } ``` ### 应用服务层 在 Service 层中加入 `@Transactional` 注解以确保事务一致性[^2]。这有助于保证一系列数据库操作要么全部成功提交,要么完全回滚。 ```java @Service @Transactional(readOnly = true) public class UserService { @Autowired private UserRepository userRepository; public Optional<User> getUserById(Long userId){ return userRepository.findById(userId); } @Transactional public void saveOrUpdate(User user){ userRepository.save(user); } } ``` ### 数据库版本控制 借助 Flyway 工具可方便地实施数据库模式变更脚本的应用程序生命周期管理,从而保障应用程序和其背后的数据模型始终保持一致状态[^3]。 ```properties # application.properties or application.yml spring.flyway.enabled=true spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=password ``` 以上配置展示了如何设置连接到本地运行的 MySQL 数据库并启用 Flyway 自动执行 SQL 脚本来更新架构。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值