Replacing a full ORM (JPA/Hibernate) by a lighter solution : Recommended patterns for load/save?

前端 未结 8 1761
孤城傲影
孤城傲影 2021-01-29 20:26

I\'m developing a new Java web application and I\'m exploring new ways (new for me!) to persist the data. I mostly have experience with JPA & Hibernate but, except for simpl

8条回答
  •  独厮守ぢ
    2021-01-29 21:09

    Not sure if it's a bit late. Maybe it's worth to have a try. Here is the solution provided by library abacus-jdbc:

    Person person = Person.builder().firstName("jdbc").lastName("whoiam").build();
    long personId = personDao.insert(person);
    
    City city = new City(123, "Sunnyvalue");
    int cityId = cityDao.insert(city);
    
    Person person = Person.builder().firstName("jdbc").lastName("whoiam").build();
    long personId = personDao.insert(person);
    
    Address address = Address.builder().street("1130 Ky").cityId(cityId).state("CA").zipCode("95677").personId(personId).build();
    addressDao.insert(address);
    
    Person personFromDB = personDao.gett(personId);
    System.out.println(personFromDB);
    
    personDao.loadJoinEntitiesIfNull(personFromDB, Address.class);
    
    System.out.println(personFromDB);
    

    Basically you can do CRUD and a lot of more without writing single DAL/DAO method. All the DAO classes you need to define are:

    public interface PersonDao extends JdbcUtil.CrudDao, 
                                JdbcUtil.JoinEntityHelper {
    }
    
    public interface AddressDao extends JdbcUtil.CrudDaoL, 
                                        JdbcUtil.JoinEntityHelper {
    }
    
    public interface CityDao extends JdbcUtil.CrudDao {
    }
    

    I know it doesn't address all your questions/problems. But at least it provides some alternative approaches for couple of your questions. If you need a framework which provides the extract features you asked, no doubt, that's Hibernate. But sometimes, if you want to control the db operations by a few very simple methods, rather than depend on a framework. Try abacus-jdbc. Here is all the sample code on Github

    Disclaimer: I'm the developer of abacus-jdbc

提交回复
热议问题