Need a code-generation tool for entities' DAO's [closed]
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago开发者_高级运维.
Improve this questionI have lots of entities in a project and I need to make a data-access-object for every one of them. I think those DAO's will be stateless session beans, each having an entity manager injected into it (I'm not sure on this one, but since I'll be using them in the context of JBoss AS, it looks reasonable. Any advice?).
Is there any tool (ideally, a maven plugin) that can scan my entity classes, process their annotations and generate those beans for me? Each bean should expose methods to create or delete entities, as well as each of the named queries of its respective entity class as a separate method. To be more precise, I want something like in this article: http://community.jboss.org/people/ilya40umov/blog/2011/01/06/genericejb-based-jpa-entitymanager-extension but with different methods for each named query. I'll be using JPA 2.0, with Hibernate 4.0. Thanks in advance! Edit: Stupid me, I forgot to mention that I'm not using spring, and the term DAO is just used to illustrate what I want...Telosys Tools is designed for this kind of task It generates code from the database model
See https://sites.google.com/site/telosystools/
You can use a part of this tutorial https://sites.google.com/site/telosystutorial/springmvc-jpa-springdatajpa to generate only the JPA part
You can also create your own templates in order to match you own needs (or adapt existing templates)
you could use generics and reflection to make something like that
public interface IDao<T> {
public <A extends Serializable> T getElementByID(A x);
public Long getRowsCount();
public List<T> getAll();
public List<T> getAll(String order);
public void saveOrUpdateElement(T x);
public void updateElement(T x);
public void saveElement(T x);
public void deleteElement(T x);
public void setClase(Class<T> clase);
public Class<T> getClase();
public void mergeElement(T x);
public T getFirst();
}
the method public void setClase(Class clase); make all de magic so if you need to query to x then you set the class and the implementaton for example of getAll() will be
public List<T> getAll(){
return session.createQuery("from "+getClase().getSimpleName()).list();
}
Take a look at Spring-Data-JPA or Spring ROO
精彩评论