dao

Getting fewer columns with hibernate

和自甴很熟 提交于 2019-11-30 22:15:15
I have a table with 11 columns, but I need to get only 2 of them in my application, I'm using spring/hibernate/DAO combination. For now I have a domain class which includes all 11 fields, and mapping file which maps all 11 columns in table. How do I use get just 2 of them not all? Either: Use projections - Pro: nothing to add - Con: Not typesafe (the result is a List of rows where each row is an Object[]) : select f.foo, f.bar from FatEntity f Use a constructor expression in the SELECT clause (the specified class is not required to be an entity or to be mapped to the database) - Pro: typesafe

How to create a Generic DAO class using Hibernate Context sessions

我怕爱的太早我们不能终老 提交于 2019-11-30 22:08:02
问题 I'm trying to implement a Generic DAO using the Hibernates Context Sessions. Following was my shot:| import java.io.Serializable; public interface GenericDao<T, ID extends Serializable> { /** Persist the newInstance object into database */ ID create(T newInstance); /** * Retrieve an object that was previously persisted to the database using * the indicated id as primary key */ T read(ID primaryKey); /** Save changes made to a persistent object. */ void update(T transientObject); /** Remove an

Java Remove repeated try, catch, finally boilerplate from DAO

青春壹個敷衍的年華 提交于 2019-11-30 21:51:19
I have a DAO class with many methods that have a lot of repeated code along the lines of: - public void method1(...) { Connection conn = null; try { //custom code here } catch (SQLException e) { LOG.error("Error accessing the database.", e); throw new DatabaseException(); } catch (QueryNotFoundException e) { LOG.error("Error accessing the database.", e); throw new DatabaseException(); } finally { if (conn != null) connectionPool.returnConnection(conn); } public void method2(...) { Connection conn = null; try { //different custom code here } catch (SQLException e) { LOG.error("Error accessing

What's an appropriate DAO structure with jpa2/eclipselink?

こ雲淡風輕ζ 提交于 2019-11-30 21:50:55
I've JPA entities and need to perform logic with them. Until now a huge static database class did the job. It's ugly because every public interface method had an private equivalent that used the EntityManager, to perform transactions. But I could solve that having a static em too! However i'm wondering if that's an appropriate design, especially as the class is responsible for many things. Not surprisingly, the code i found online of real projects was not easy to understand (i might then as well remeain with my code). The code here is easy to understand, although maybe over generic? Anyway, on

“Type of the parameter must be a class annotated with @Entity” while creating Generic DAO interface in Room

可紊 提交于 2019-11-30 18:58:29
I am using Room architecture component for persistence. I have created generic DAO interface to avoid boilerplate code. Room Pro Tips But my code doesn't compile saying "Error:(21, 19) error: Type of the parameter must be a class annotated with @Entity or a collection/array of it." for the Generic class T. interface BaseDao<T> { @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(T... entity); @Update void update(T entity); @Delete void delete(T entity); } @Dao public abstract class ReasonDao implements BaseDao<ReasonDao> { @Query("SELECT * from Reason") abstract public List<Reason>

What's an appropriate DAO structure with jpa2/eclipselink?

隐身守侯 提交于 2019-11-30 18:25:15
问题 I've JPA entities and need to perform logic with them. Until now a huge static database class did the job. It's ugly because every public interface method had an private equivalent that used the EntityManager, to perform transactions. But I could solve that having a static em too! However i'm wondering if that's an appropriate design, especially as the class is responsible for many things. Not surprisingly, the code i found online of real projects was not easy to understand (i might then as

What's the difference between DAO and Data Mapper

一世执手 提交于 2019-11-30 15:55:04
问题 Is there a difference between the DAO pattern and the Data Mapper pattern? Is DAO just one of doing Data Mapper? 回答1: I wouldn't actually call DAO a "pattern". As I see it, DAO is pretty much what it is -- a Data Access Object", which encapsulates the details of accessing a persistent data store and generally speaking has nothing to do with the database: interface IBlogDaoService { Blog GetBlog(long id); void SaveBlog(Blog blog); } It's clear that implementations can use either DB (in which

What's the difference between DAO and Data Mapper

拈花ヽ惹草 提交于 2019-11-30 15:30:29
Is there a difference between the DAO pattern and the Data Mapper pattern? Is DAO just one of doing Data Mapper? I wouldn't actually call DAO a "pattern". As I see it, DAO is pretty much what it is -- a Data Access Object", which encapsulates the details of accessing a persistent data store and generally speaking has nothing to do with the database: interface IBlogDaoService { Blog GetBlog(long id); void SaveBlog(Blog blog); } It's clear that implementations can use either DB (in which case it's quite logical to use a Data Mapper), or simple XML file storage mechanism. The Data Mapper on the

Junit test case for database insert method with DAO and web service

怎甘沉沦 提交于 2019-11-30 11:40:58
问题 I am implementing a webservice based university management system. This system adds certain courses to database. here below is the code that I am using. Course.java public class Course { private String courseName; private String location; private String courseId; public String getCourseId() { return courseId; } public void setCourseId(String courseId) { this.courseId = courseId; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this

DAO Unit testing

十年热恋 提交于 2019-11-30 11:03:51
问题 I have been looking at EasyMock and tutorials/examples around using it for Unit Testing DAO classes, for an "outside container" test. However, I think most of them talk about testing the Service Layer instead, mocking the DAO class. I am a bit confused, is it really how you Unit Test the DAO layer? Some would say that the tests interacting with DB & EJBs are actually Integration tests and not Unit tests but then how would you know if your SQL is correct (assuming no ORM) and your DAO inserts