문제

I've been developing web app using Spring MVC 3 with Hibernate for persistance. Basically, my problem is that there is a requirement that there are two separate projects (one for end-users and completely other for admins). However, both applications should refer to same database.

I've been thinking of creating three separate projects: - one would be basically Hibernate project with models, repositories and services - second would be Spring MVC project for admins - third would be Spring MVC project for users

So my question - is it possible so somehow make reference to this Hibernate project from both of my Spring MVC projects, cause all I want to use are those services for data accessing.

Thanks in advance!

도움이 되었습니까?

해결책

Yes you can, but not a reference to the hibernate project itself, You could easily build some Jar file inside your Hibernate project using Ant Script, this Jar file would encapsulate all your common classes an resources

Then you could add this Jar to Internal and Admin Project Build paths

The problem of this solution is that you will need remember to build the common Jar file on every change on your common classes

Check this tutorial to know how building a Jar using Ant, you could use Maven too

There is another approach using SVN externals or any mapped feature on your Version control system, by making the common project as an external repository and check it out in both projects (internal and admin). However using this approach will have the following problems:

  • The configuration of this process is not easy as the first approach
  • Your code is copied three times, one in the common project, the two others are in the internal and admin project checkouts, the same code the same files.
  • Do not ever surprised when you find yourself editing one of the common files with no effect in the running project just to discover at the last minute that your were editing the one of the other two copied files :)
  • Someone with some experience with this approach has wrote about his bad journey here

다른 팁

In addition to Ant, you could also use Maven, which supports projects that contain sub-projects, and has plugins to generate and maintain your Eclipse projects for you. You could make the Hibernate stuff one JAR sub-project, then make the two sites WAR projects, all under a POM base project.

If you do this manually, I would suggest hooking your projects together within the IDE so that your debugger works.

On gotcha: If you make your Hibernate JAR only Hibernate, you will essentially be running 2 Hibernates pointed to the same DB, which will NOT work out of the box. The problem is that, buy default, Hibernate likes to control the DB and do lots of caching. So, for instance, if you read in a domain object once, then in another piece of code read the same object, theoretically Hibernate will check to see if it has already cached the object, and if so use that one. The end result is that if you have two Hibernates reading and writing to the same set of tables, they could easily get out of sync.

There are some ways around this problem. One way might be to setup a shared cache between the two, so that reading will check the shared cache. This is the typical approach when you have clustered environments where the same code is running on multiple app servers, but all accessing the same tables. The downside here is that you will now have a more complicated setup.

Another approach might be to configure Hibernate to be more paranoid. This is typically done when you have a situation where something outside of you app can write to the tables, like maybe a batch process or something. Hibernate can be configured to ignore cached reads and do version checking prior to writing. The downside here is that you are giving up some, maybe even a lot, or performance.

The last approach, and in my opinion my personally preferred one, would be to write a service layer to access the code that operates as a stand-alone application. How you do this will highly depend on what you are comfortable with, how you want to integrate, and any architectural decisions you must consider. In this approach, you would write your service layer that uses Hibernate under the covers, and somehow publish an "API" that your web apps can use. The API can be anything from JMS (my preferred if all the code is running under the same app server), RESTful or non-RESTful web services, or even some remote method calling mechanism like RMI or CORBA. Depending on the implementation, this can really add the ability to expand your applications in the future.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top