GeeCON Prague 2014*
The moose likes OO, Patterns, UML and Refactoring and the fly likes UML to generated Java (and sql)? Does this work? Good tools? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "UML to generated Java (and sql)? Does this work? Good tools?" Watch "UML to generated Java (and sql)? Does this work? Good tools?" New topic
Author

UML to generated Java (and sql)? Does this work? Good tools?

Jochen Szostek
Ranch Hand

Joined: Mar 23, 2008
Posts: 42

Hi all,

I was wondering if it's good practise to generate Java and/or sql from UML?
Do you do things like this? What tools can you recommend?

Basically by moddeling your UML the whole Hibernate/Jpa/Java/sql stuff could be automated I guess.



What I've done so far concerning this is drawing something UML like (DB structure) with MySql workbench...forward engineering my DB.
Then in MyEclipse I've generated the whole Java code. Which I didn't use completely but I copy pasted lots of the code in my manually written DAO code.

But maybe there are better workflows around concerning this? Perhaps with full roundtrips so everything gets updated? (ok ok, maybe I'm just dreaming here )

Any thought or comments on this more then welcome!

Kind regards,

Jochen
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I find that modeling in UML very quickly gets burdensome, when the model gets bigger. Textual representations work much better for me. In a past project, we created our own small XML language for specifying the model, and a simple generator that created the code artifacts. Wasn't hard to do, gave us a lot of flexibility and saved us a lot of grieve.

I have never seen full round trip work well - haven't tried very hard, though.

The most important thing with one-way code generation is that you *never* *ever* touch the generated code - with other words, keep the generated code cleanly separated from manually written code. You need to be able to regenerate it from scratch any time without fearing to loose manual changes. If you need to add to the generated code, using inheritance has worked well for me - let the production class extend the generated class.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi,

I've had pretty much the same experience as Ilja!

I've used (an older version of) Borland together which basically works but is not too helpful if you want to generate more than basic class scaffolds. The NetBeans UML tool works similar well or not too well. Moreover I've heard that MID Innovator was used very successfully for a big project but I've never used it myself.

What works really well for generating code and other artifacts (but without UML input) is Ruby on Rails, Grails and Seam-Gen from the JBoss Seam project.

But as Ilja pointed out, modelling every detail of bigger projects in UML is a lot of additional work if you can't fully use it to generate your code. Together with the problem that you will soon get in trouble if you want to make changes to the generated code. In my opinion this was just a dream some years ago that you should be able to create a complete project with a single click on a button in any programming language you want, but it failed. In practice here are simply to many problems you have to deal with depending on your tools and programming language in a real world project.

Marco
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Keep in mind that the Unified Modeling Language is a design tool that is used at "design" time. It is not a tool best suited for "implementation" or "coding."

One can easily "design" an application's class structure with a UML tool such as JUDE. And then once the design is "workable", you can generate Java code from the design. And then continue to write the code needed. Note that you never go back to regenerating the skeleton Java code again. The UML tool can help easily generate 50 to 100 class files with a click of a mouse. This helps streamline the transition from a visual OO design phase to the implementation/coding phase.

UML is a great tool and helps when designing large systems with many complex parts. Anything with less than 100 classes doesn't warrant too much design with UML, in my opinion. One of the best parts about UML is that the diagrams serve as good documentation that can (1) help teach others, (2) communicate design, and (3) help programmers remember how something complex works three or four years later.

Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

To have some UML diagrams for documentation reasons is definitely a very good point! It would have been helpful in many, many situations to have at least some small UML diagrams to get a brief overview of some complex parts of an application.

From your answer I guess you have quite a bit practical experience with this. So just out of curiosity: How do you handle bigger projects with 100+++ classes? Do you break everything down into modules or are some tools good enough to help you manage diagrams of that size? My personal experience is that UML diagrams are most helpful to visualize details of a smaller part or module of an entire application (mostly with class diagrams) but I can hardly imagine to model a really big project with them.

Which of all the available types of UML diagrams do make sense for practical use in bigger projects in your opinion?

And if you say UML is most helpful for the transition from design to code then how do you handle iterative changes which you obviously will have if you don't use a simple waterfall model as development process?

Marco
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
To have some UML diagrams for documentation reasons is definitely a very good point! It would have been helpful in many, many situations to have at least some small UML diagrams to get a brief overview of some complex parts of an application.


Yes. This is one of the greater benefits of using UML diagrams for designing software. It is important to understand that software design and software programming are different processes. UML is a design tool, not a tool for coding or programming.

From your answer I guess you have quite a bit practical experience with this. So just out of curiosity: How do you handle bigger projects with 100+++ classes? Do you break everything down into modules or are some tools good enough to help you manage diagrams of that size? My personal experience is that UML diagrams are most helpful to visualize details of a smaller part or module of an entire application (mostly with class diagrams) but I can hardly imagine to model a really big project with them.


Typically, classes do not exist before the design process. They are a product of the design process. Everything in an application does not have to been diagrammed or designed with UML. In some cases, you might design something, then write the code, and then design a little bit more and add any new stuff to existing diagrams. And, even more radical, maybe some elements don’t exists anywhere in any diagram. The main point here is that the most complex scenarios benefit from the visual design process. It is extremely difficult to design anything complex without some sort of visual abstraction, especially if there are multiple programmers involved. How can the architect efficiently make sure that everyone is working on the same concepts/design?

It is important to highlight that a diagram is just a guide and it might not match exactly with the code. It doesn’t have too. If you want to know exactly what is in the code, then read the code. Diagrams outline the design, not exact implementation details.

Which of all the available types of UML diagrams do make sense for practical use in bigger projects in your opinion?


Sequence diagrams and Class diagrams.

And if you say UML is most helpful for the transition from design to code then how do you handle iterative changes which you obviously will have if you don't use a simple waterfall model as development process?


Software design is an art, not a science. Strong design skills are difficult to acquire and build upon. This is not something that you can easily search "Google" and find the "correct" answer for. A foundation of OO design skills typically takes a couple of years to develop and then requires more time and active engagement to get stronger. If you get it, then you will develop a style that is "best" for you and has worked well in the past for "you" and will be something that you can "teach" to others (in the real world, not via Intenet postings).

Jochen Szostek
Ranch Hand

Joined: Mar 23, 2008
Posts: 42

Thanks a lot everybody for the replies.

I indeed agree you can't generate an application via UML but it does help you get started faster coding the business models I think.

Are there any well known tools that are worht mentioning?

I tried MyEclipse and it was ok. But perhaps there's a lot of better gear around?

Thanks and kind regards,

Jochen
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
http://jude.change-vision.com/jude-web/index.html
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: UML to generated Java (and sql)? Does this work? Good tools?