I have created dao tier in my application. Now I want to generated web tier with basic crud operations in wicket. I read that apache ISIS can do this: http://isis.apache.org/. I successfully download and run their example and it look very good but I didnt find any tutorial how to replace their DAO tier with mine. So the question is how to replace it or is there better framework which can handle this task ?
Apache Isis is a full-stack framework, so you don't really need to write your own DAO layer. Instead, we leverage the JDO/DataNucleus ORM. Annotate your pojos and the ORM will do its thing.
The only persistence layer stuff you do need to write is specific implementations of your repositories, to use JDO queries. The example archetype that you have already tried out has an example of this.
If you have an existing database schema, you can adapt the JDO/DN ObjectStore to use it by using good old fashioned SQL views. For any non-updateable views, you can install "instead of" triggers. In fact, I'd argue this is good practice even for green-field projects.
Related
We started writing a Java Framework for our company. But we don't have enough experience about Java. We decided to use JPA framework for database CRUD operations.
What do you suggest about that:
about defining persistence.xml. We search creating dynamic
EntityManager and found some documents but we don't know that is
it best way.
Is it a good way that create a layer over JPA base db operations?
(for example CRUD methods.)
How can we do calling JPA CRUD methods from my CRUD methods in
framework?
We will use this framework for desktop and web applications. Is
deployment a problem for us.
Do we have to use EJB?
Is there alternative to JPA which you suggest? (example: ADF,JDBC)
Thanks
It highly depends on your requirements and what you want to do with your "framework". I do not know enough of your project to give you a real advise, but here are some thoughts:
What do you mean with "framework"? Are you developing a library which other people should use? What should be the purpose of your framework? Is it a data access layer for some of your company data? If so: JPA is a kind of a standard and might be a good fit since it is widely used. If other people should use your "framework" it is good to use something which is standard and used in many other applications and tools.
Do you really need a data access layer on the desktop? Do you have a rich client? It is no problem to just "deploy" the application to the desktop, but a data access layer must always be configured and (maybe) updated. And that's where the pain begins when you use a rich client. Users must configure a database, the database must be installed or accessible remote and the version of the client must match the version of the database. Sooner or later this will hit you.
What else have you considered already? What about a ORM? Hibernate might by a good and popular fit. Also eBeans which is used in Play! is very cool. If you make a CRUD applications, frameworks like eBeans are doing most of the work out-of-the-box for you. You create a model (just POJOs + annotations) and the frameworks provides the complete data access layer (including the database setup).
I have a Java web application that has a 'disconnected' Java Swing desktop app. Using the desktop app users connect to the internet and download data they need from a server. They can disconnect and use the application offline. When they reconnect to the internet they can sync their data back to the server.
The server itself is a Java EE web application and the desktop app is a limited functionality version of the web app. Up until now all business logic and data access code was coded separately for each application. The data stores were different for each application (Web is SQL Server and Desktop is Serialized objects). Some new requirements involve a lot of development to both applications. Since the functionality is the same I want to layer the data access code and business logic so I can easily reuse it for both applications.
My plan is to do the following.
1) Create a DAO library (JPA)
Switch my DAOs (currently JDBC) to use Java Persistence API. This way I can start using a RDBMS on the desktop app like derby, sql express or something similar and reuse the entites and DAO code to do all of the db operations. I can bundle this into a class library and use the same library for the web and the desktop.
2) Create a library for business logic
The web app is written in struts 2. I will take all of the logic in my Action classes and but it in POJOs. Create a jar class library with the business logic. I can add the lib to my web and desktop project. Then I can call the POJOs from my struts actions and from my desktop application.
I assume that the JPA will take care of duplicating the data access code and putting the business logic (which for the most part are dao calls) in a separate library will take care of duplicating the business logic.
So my question is: What is a good strategy for separating layers for an application that can be used online and offline?
If you have any recommendations for achieving this differently, any warning for me as I begin this project or any frameworks that might help me please let me know.
What is a good strategy for separating layers for an application that can be used online and offline?
Well, your plan looks decent from a high-level point of view and will definitely simplify the maintenance of your applications. I don't have much to add: create JPA entities, a service layer, and if you feel the need, a DAO layer1. Then use these parts in both of your applications, with different JPA settings.
Here are a few additional notes:
I would go for a full Java database on the desktop (derby), it will be easier to manage its lifecycle.
Spring would provide good overall support (for layering, for declarative transaction management).
JPA doesn't provide any facility for the merge or databases, you'll have to implement that yourself and handle painful things like conflicting changes, etc.
1 I would actually even consider skipping the DAOs and accessing JPA's EntityManager directly from the service layer. Some people might not agree with this but the fact is that the EntityManager already implements the Domain Store pattern which does pretty much what the DAO pattern does and and there is not much value at shielding it behind a DAO.
When developing Java EE applications how do I separate Business Logic so it can be reused?
I inherited an application that is mostly Model 1. Business logic is located in JSPs, Servlets and DAO code.
I want to separate the business logic but I am confused by all of the frameworks etc. that exist.
I am looking into Hibernate with JPA to handle all database persistence. Currently all SQL is hand coded and separate SQL is used for different RDBMS. My DAOs will call the code necessary for persistence.
I am thinking of using Struts for my web layer. The part I don't understand is the Business Logic.
I don't want my logic tied to the Web Layer because I want to reuse the logic in a Java SE application.
I thought about putting business logic in Entity classes but that seems like a bad idea.
Is there some technology or pattern that can be used as a guideline for creating reusable business logic?
If I am not clear I will edit.
Thank you.
To separate your frontend code (the view) from your business logic (controller) and your data (model) you can follow the MVC pattern.
You can have your controllers access other classes that contain the reusable business logic that will be used within your Java SE applications.
There are a lot of frameworks that help you to build web applications in this style like Grails (uses Groovy), Play or Roo. But because you said 'enterprise' you should have a look at the Spring framework and its MVC module. Spring offers good integration with Hibernate and allows you to follow the MVC pattern with your web applications.
I would say take it piece-meal. Solve the biggest problems first, which is in your case having business logic in the jsp pages. You can accomplish this using any web MVC framework of your choice (Struts, Spring MVC, Grails are all good. Pick one that you are most comfortable with).
The next problem is organizing your business logic in a separate Model layer that your controllers can invoke. Spring is a good DI framework for organizing and bootstrapping your application. Also, Spring supports a number of web MVC frameworks including Struts, JSF etc.
The last problem is your Dao layer. You mentioned you want to use Hibernate/JPA. I dont know how familiar you are with Hibernate, but make sure that you are trying to solve an actual problem by switching to Hibernate (since switching to Hibernate usually comes at a significant cost and headaches).
I've been working on a java web project.
Currently this project doesnt use any framework. Its a standard MVC application, using servlets and DAO with jdbc access to database (all queries are handwritten).
The project has a good code (all project developed using TDD), but its way too slow to add any feature, since all have to be done by hand.
In this case, which framework would you suggest to add this project?
I can't use a framework that requires me to rewrite all current code base to fit in this framework.
I think that Hibernate is a great choice for persistence.
But what else? Spring? VRaptor? Struts?
You might add interfaces for your classes, use Hibernate for the persistence layer replacing your DaoSqlImplementation by DaoHibernateImplementation one per time. As long as you wire your application with interfaces you won't have any problem.
Also I recommend you to use Spring, this way you can switch between implementations declaratively by just modifying the XML. One of the principles Spring follows is IoC (Inversion of Control). In this case means your application controls the framework and not the framework controls the application which is exactly what you requested.
One important thing is that you must justify every framework you decide to add to the application and not just add it because it is very cool.
may i know in eclipse, is there any feature that will auto generate domain objects with all table relationship properly mapped in class?
can provide me with some reference articles on this?
You can use something like Hibernate to accomplish this
This plugin set for Eclipse called Hibernate Tools for Eclipse and ANT will do most of the work for you.
In particular it will do Reverse Engineering: The most powerful feature of Hibernate Tools is a database reverse engineering tool that can generate domain model classes and Hibernate mapping files, annotated EJB3 entity beans, HTML documentation or even an entire JBoss Seam application in seconds!
Telosys code generator does this kind of job.
It's an Eclipse plugin, it uses the database schema to create a light model
that is used to generate the Java code.
There are some predefined templates available on GitHub (for JPA, POJO, Documentation, Spring MVC, etc )
See http://www.telosys.org
and http://marketplace.eclipse.org/content/telosys-tools
Templates : https://github.com/telosys-templates-v3
Articles about code generation with Telosys :
https://modeling-languages.com/telosys-tools-the-concept-of-lightweight-model-for-code-generation/
https://dzone.com/articles/telosys-a-code-generation-tool-by-laurent-guerin
You can use Hibernate Tools 3.0.0.GA either via Eclipse or ANT to auto-generate your hibernate domain entities directly from your database tables.
See tutorial here :
http://docs.jboss.org/tools/3.0.0.GA/en/hibernatetools/html_single/index.html
I have a solution for you i.e to create auto generate domain objects with all table relationship properly mapped in class ...Try Dal4j yes you can find it in sourceforge.net/p/dal4j/wiki/ DAL4j is a Command Line and Framework tool that can be used to reverse engineer a MySQL or SQLServer database schema into a set of JPA Entity Beans.
DAL4j can be useful for scenarios where there is an existing database schema but a technology other that JPA is used by applications to interact with the database. DAL4j can provide an easy way to migrate your code base from other technologies such as JDBC or Hibernate to JPA.
The beans generated can be 1 or two types: Simple or Framework. Simple beans are standard pojo classes managed by your application using JPA semantics. Framework generated pojos use the DAL4j framework DAO generic to simplify CRUD operations.
DAL4j provides optional hooks to allow you integrate encryption/decryption of data fields that must be encrypted in the database.
Last, DAL4j provides a set of Generic classes that can be used to simplify creation of Session Beans which perform CRUD operations using generated Entities.
I think you will find this article feasible....
You want an object relational mapping of which Hibernate is the most popular for Java. The hibernate tools are typically better for taking annotated classes and using them to generate a schema, as opposed to vice versa, which is what you sound like you're doing. I suspect you'll be doing a lot of hand-annotating if you're working with a legacy DB schema.
if you use grails, you can generate domain objects with GRAG http://sourceforge.net/projects/grag
I use eclipse for java development, but when it comes to generating domain entities I use Net beans.
Create an EJB module, and then right click and generate entities.
You need to set up the database also you can select the tables you want visually.
Regards
Lyju
It feels like another rather common question that people always run into.
The link below links to a blog detailed enough for me to learn how to generate entities from database schema the first time.
http://shengwangi.blogspot.com/2014/12/how-to-create-java-classes-from-tables.html
Just in case, the following link refers to eclipse help page. This link should never expire:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jpt.doc.user%2Ftasks021.htm
I downloaded JBoss and failed to understand how it works. I think the plugin that I used is Hibernate Tools but I am not sure as I did not install any new plugin for this purpose. I am using Eclipse Luna for EE.
Hope this helps.
I got so tired of manually coding this kind of stuff so I made a tool to generate models, dao, and dao implementation from a schema. It's oriented towards spring boot and only tested on MySQL, but for those that don't want to use Hibernate and just want to work with jdbc/sql and JdbcTemplate, or just want POJOs with getter/setters generated for tables, then this could perhaps be something to kick off the coding.
Called Jassd (Java Automated Spring Source-code for Databases generator), I'm "jazzed" to introduce this tool: https://github.com/aforslund/jassd