-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 
Author Message
 Post subject: How do I know what columns have default values?
PostPosted: Tue Jul 11, 2006 6:34 am 
Regular
Regular

Joined: Wed Sep 28, 2005 6:45 pm
Posts: 56
I am trying to reverseengineer a rather large database (600+ tables) and it has quite a lot of fields with default values. Quite many of these are set to now() (which sets a timestamp).

I would like to be able to set all these fields to insertable = false, but I don't really know where to start..

Some of the columns have default values like '' but I would like to just ignore those...

Thanks in advance for any help :)

(One more thing, any better suggestions than setting those fields to insertable = false? I would really like to use the databases time since we have multiple clients connecting to the database through different connection pools etc... Or otherwise handle columns with default fields?)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 8:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
we currently don't read into default's; we could do that though but i'm not keen on doing it since it is hard to define what the default behavior should be here (except just do what we do now which is ignore ;)

setting insertable false can currently only be done by you handling it in the hbm.xml templates directly.

btw. insertable="false" sounds like a perfect solution for your issue.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 9:43 am 
Regular
Regular

Joined: Wed Sep 28, 2005 6:45 pm
Posts: 56
Thanks for the reply...

Is there no way to do this with custom DelegatingReverseEngineeringStrategy? Or a hack om a template file or something like that? :o)

I do forsee having to reverse engineer the database more than once, so it would be a hassle to do it manually every time, especially with the 600+ tables...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 11:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you can always hack the templates ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 16, 2006 10:27 am 
Regular
Regular

Joined: Wed Sep 28, 2005 6:45 pm
Posts: 56
Ok, have been trying to look into the matters, but without finding a solution.

In addition I have found some additional stuff that would be practical:

toString() seems to be implemented in the ftl files, but I don't see any way to turn the generation for it "on"?

There are currently two constructors generated, a default constructor and a full constructor. A constructor that took the primary key as parameter would be really practical too.

Let us say you want to set the contact on an order:
Instead of writeing:
Order order = new Order();
Contact contact = new Contact()
contact.setContactId(new Integer(3) );
order.setContact(contact);

You could write:
Order order = new Order();
order.setContact(new Contact(new Integer(3) ) );

Which is half the code and you don't have the temp Contact object lying around...

The code for the constructors seems to be in PojoConstructors.ftl, but I once again, I am not familiar enought with the code here to actually be able to hack the templates as I would want...

Thanks in advance for any help :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 16, 2006 10:27 am 
Regular
Regular

Joined: Wed Sep 28, 2005 6:45 pm
Posts: 56
Ok, have been trying to look into the matters, but without finding a solution.

In addition I have found some additional stuff that would be practical:

toString() seems to be implemented in the ftl files, but I don't see any way to turn the generation for it "on"?

There are currently two constructors generated, a default constructor and a full constructor. A constructor that took the primary key as parameter would be really practical too.

Let us say you want to set the contact on an order:
Instead of writeing:
Order order = new Order();
Contact contact = new Contact()
contact.setContactId(new Integer(3) );
order.setContact(contact);

You could write:
Order order = new Order();
order.setContact(new Contact(new Integer(3) ) );

Which is half the code and you don't have the temp Contact object lying around...

The code for the constructors seems to be in PojoConstructors.ftl, but I once again, I am not familiar enought with the code here to actually be able to hack the templates as I would want...

Thanks in advance for any help :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 16, 2006 4:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
afaik, if you use assigned id's (the only real situation where id's are known at construction time) we do include the id in the constructor.

tostring is generated if you have a field marked with <meta attribute="use-in-tostring">true</meta>

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 17, 2006 2:48 am 
Regular
Regular

Joined: Wed Sep 28, 2005 6:45 pm
Posts: 56
Hmm....

I am as I mentioned earlier reverse-engineering a database with 600+ tables and would like to use the toString... I assume I would have to add the use-in-tostring value true to all the columns in all the tables?

To have that toString() method I would have to write that meta tag 6000 times or something...

It would be really nice if you could just set a global value or something makeing all columns appear in the toString method, and you could set the meta tag to false if there were some columns you didn't want to have in the toString method...

About the primary key not beeing available except if you use assigned id's, well.. we do actually have quite a few situations where you know the primary key of an object, but you haven't loaded the object into memory... Maybe not a good solution, but it would be nice to be able to have the constructor I mentioned anyway...


Last edited by Blackbrrd on Mon Jul 17, 2006 2:59 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 17, 2006 2:48 am 
Regular
Regular

Joined: Wed Sep 28, 2005 6:45 pm
Posts: 56
... and the double post came because I got an error message while posting, I did it last time too...

The error message was this:
Failed sending email :: PHP ::

DEBUG MODE

Line : 234
File : /www/forum.hibernate.org/htdocs/includes/emailer.php

It didn't come when I just updated my post...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 17, 2006 4:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Blackbrrd wrote:
I am as I mentioned earlier reverse-engineering a database with 600+ tables and would like to use the toString... I assume I would have to add the use-in-tostring value true to all the columns in all the tables?

To have that toString() method I would have to write that meta tag 6000 times or something...


Just write it once in property.hbm.ftl and you should be fine ;)

Quote:
It would be really nice if you could just set a global value or something makeing all columns appear in the toString method, and you could set the meta tag to false if there were some columns you didn't want to have in the toString method...


that is what the meta attribute support should do.

Quote:
About the primary key not beeing available except if you use assigned id's, well.. we do actually have quite a few situations where you know the primary key of an object, but you haven't loaded the object into memory...


so do:

x.setY(session.load(Y.class, 42));

Anything else has bad side effects and thus i won't put it into the tool as an easy thing to do ;)

Quote:
Maybe not a good solution, but it would be nice to be able to have the constructor I mentioned anyway...


The templates are free for you to edit and enhance to do just this.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 18, 2006 10:32 am 
Regular
Regular

Joined: Wed Sep 28, 2005 6:45 pm
Posts: 56
"Just write it once in property.hbm.ftl and you should be fine ;) "

Hmm... I am using annotations so that might not be the solution there?

If it is, I didn't get exactly what to write where... might just be me beeing a bit dense.. ;)

could you just edit this for me?



Code:
    <property
        name="${property.name}"
        type="${property.value.typeName}"
<#if !property.updateable>
        update="false"
</#if>
<#if !property.insertable>
        insert="false"
</#if>
<#if !property.basicPropertyAccessor>
        access="${property.propertyAccessorName}"
</#if>
<#if property.lazy>
        lazy="true"
</#if>
<#if !property.optimisticLocked>
        optimistic-lock="false"
</#if>
<#if property.value.hasFormula()>
<#assign formula = c2h.getFormulaForProperty(property)>
<#if formula?has_content>
        formula="${formula.text}"
</#if>
    />
<#else>
    >
  <#foreach column in property.columnIterator>
        <#include "column.hbm.ftl">
  </#foreach>   </property>
</#if>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 18, 2006 1:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
well if you want annotations directly generated you will have to wait for <meta> support in reveng.xml.

If you can "survive" going over two steps you can insert a

<meta name="use-in-tostring">true</meta> just after the end > property so it will something like

<property ...>
<meta name="use-in-tostring">true</meta>
...
</property>

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 3:29 am 
Regular
Regular

Joined: Wed Sep 28, 2005 6:45 pm
Posts: 56
Ah, so I would have to generate the hbm files and then make the java files from the hbm fil?

On the same general subject, use-in-equals has to be used in the same way, right?

Is there any reason not to generate the equals/hashcode methods? They seem to be very useful for instance if I would want to check if a user has made any changes to the data without resorting to a database roundtrip and I already have the data. (its for a application with a client/server architecture where there is quite a bit of latency between the client and server)

On another subject
I have a collection that I always want to have in ordered mode, like this:
Code:
   @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "torder")
        @OrderBy("orderstatussnapdate DESC, orderstatussnapid")
   public List<Torderstatussnap> getTorderstatussnaps() {
      return this.torderstatussnaps;
   }


How do I specify this in the reveng.xml files? If I can do it at all?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 3:48 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Blackbrrd wrote:
Ah, so I would have to generate the hbm files and then make the java files from the hbm fil?


Currently yes.

Quote:
On the same general subject, use-in-equals has to be used in the same way, right?


yes.

Quote:
Is there any reason not to generate the equals/hashcode methods?


Yes, there is not enough info in the jdbc metadata to decide which properties should be used.

Quote:
On another subject
I have a collection that I always want to have in ordered mode, like this:
Code:
   @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "torder")
        @OrderBy("orderstatussnapdate DESC, orderstatussnapid")
   public List<Torderstatussnap> getTorderstatussnaps() {
      return this.torderstatussnaps;
   }


How do I specify this in the reveng.xml files? If I can do it at all?


Not possible. Probably need to do as above and tweak the hbm.ftl files and possibly also the pojo templates

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.