English 中文(简体)
在春季申请中,服务层必须接受亚洲开发银行的检测。
原标题:service layer are bound to DB tecnology in a spring application

我的问题是:你的服务层是否受你使用的探测系统的约束?

例如,如果你使用秘密,你就会把一些只属于秘密特征的 h或标准序列置于你的服务层面,或者说简单地说,DAO(DAO(Dao有秘密执行,可能会被搁置执行等)?

我在为我的软件建造一个连接层的结构方面有一些困难。

EDIT This is a simple service...i think it s a service... without bound to tecnlogy i using (hibernate)

@Repository
public class PersonHibernateDAO implements PersonDAO {

    @Autowired
    SessionFactory sessionFactory;

    ... dao crud operations(implementation of PersonDAO interface) using sessionfactory ...

    //and some hibernate features methods
    public Person findByCriteria(Criterion criterion){
        // code
    }
}

@Service
public class PersonService {

    @Autowired
    private PersonDAO personDao;

    @Autowired
    private AccessDAO accessDao;

    @Transactional
    public boolean hasPermission(String username, String accessCode){
        Person p=personDao.findByUsername(username);
        Access a=accessDao.findByCode(accessCode);
        ... etc ...
    }
}

这是一项使用Dao实施服务。

@Service
public class PersonService {

    @Autowired
    private PersonDAO personDao;

    @Autowired
    private AccessDAO accessDao;

    @Transactional
    public boolean hasPermission(String username, String password){
        Person p=((PersonHibernateDao)personDao).findByCriteria(Restrictions.eq("username", username);
        ... etc ...
    }
}

这两种做法是否正确?


<><><><>>>>>>

So, to summarize what I understood:

// BASE DAO INTERFACE
public interface DAOInterface<EntityClass, IDType extends Serializable> {
    EntityClass get(IDType id);
    EntityClass findById(IDType id);
    EntityClass save(EntityClass entity);
    EntityClass update(EntityClass entity);
    void delete(EntityClass entity);
}

// AN HIBERNATE IMPLEMENTATION
public abstract class HibernateDAO<EntityClass, IDType extends Serializable> implements DAOInterface<EntityClass, IDType> {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory){
        this.sessionFactory=sessionFactory;
    }

    public void getSessionFactory(){
        return this.sessionFactory;
    }

    // Implements all DAOInterface method using sessionFactory

}

// PERSON DAO INTERFACE
public interface PersonDAO extends DAOInterface<Person, Long>{

    Person findByName(String name, String surname);
    List<Person> getInAgeRange(int year1, int year2);
}

// PERSON HIBERNATE DAO IMPLEMENTATION
public PersonHDAO extends HibernateDAO<Person, Long> implements PersonDAO{

    // Implements the methods of PersonDAO interface using sessionFactory
}

@Service
public class PersonService {

    //spring inject the correct DAO by its xml config(in this case PersonHDAO
    @Autowired
    private PersonDAO personDAO; 

    // spring manage the transaction
    @Transactional
    public List<Person> getInAgeRange(int year1, int year2){
        return personDAO.getInAgeRange(year1, year2);
    }

}

// NOW... HOW USE IT
//let s assume i have a button, pressing it a table will be populated with all persons in age range
private void actionPerfom(ActionEvent e){
    List<Person> list=personService.getInAgeRange(age1Spinner.getValue(), age2Spinner.getValue());
    //Load a table with list
}

Sorry for this wall of text, maybe can be useful for others i hope, im go in the right direction? My service layer need an interface? Is all corectly layered? I need a control layer too?

感谢。

最佳回答

我的建议:

用于大型项目,使用专用的、基于DAO的界面。 不要让你的服务层了解基本持久性技术。 使用Hibernate / JPA / JDBC / JDO / 一切仅在DAO层。

小型项目可能只需要一个服务层(特别是考虑到Hibernate <>>>/code>和JPA EntityManager<>/code>。 大部分标准农业部的行为暴露在箱子之外。

基本规则:如果你重新进行技术变革,确保你只需要改变其应用的一层。

<Update:, 此处为DAO接口样本。 你们的服务层面只能针对这一接口制定法规,而如果服务层面不需要了解,则执行会议/实体管理/支接。

public interface CustomerDao extends CommonDao<Customer>{
    Customer getCustomerByEmail(String emailAddress);
    List<Customer> getCustomersWithinAgeRange(int lowerBound, int upperBound);
}

关键:在您的服务层次上,具体说明了您的属性接口,即:

private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao){
    this.customerDao = customerDao;
}

而不是

// this is horrible, it ties the service layer to implementation
// details of the dao layer
private HibernateCustomerDaoImpl customerDao;
public void setCustomerDao(HibernateCustomerDaoImpl customerDao){
    this.customerDao = customerDao;
}
问题回答

The DAO is the place for any database specific querying - JDBC or Hibernate in your case.

服务等级是指向消费者提供像介绍级或他人这样的产品。 没有理由以数据库的具体细节对服务等级进行污染。 您的服务等级可能具有商业逻辑,但不应意识到亚洲开发银行的基本执行情况。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...