You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Skrach <da...@eunet.co.at> on 2006/03/21 15:21:28 UTC

Abator Primary Key Forcement

Hi,

I’m currently evaluating Abator (which I’m really impressed off so far!) and
came over the following limitation/idea:

During creation of Maps/DAOs/Models, Primary Key classes are automatically
created, if PK’s are found on the DB-Table. For sure, this mechanism doesn’t
work for views (which I must use for other reasons).

Therefore my question: What do you think about forced Primary Key class
creation (for Domain, Model, Map + selectByPrimaryKey() method) specified as
new element in <table ... primaryKey=“a1,a2““ ... /> inside the
abatorConfig.xml file, which take effect if no primary key is found on the
table itself?

Thanks for your help!
--
View this message in context: http://www.nabble.com/Abator-Primary-Key-Forcement-t1317590.html#a3513218
Sent from the iBATIS - User - Java forum at Nabble.com.


Re: Abator Primary Key Forcement

Posted by Skrach <da...@eunet.co.at>.
I agree that the primary key isn't guranteed when using views, my problem
seems a bit specialistic :-)

Your idea of using Abator generated Example DAO's is nice (and flexible),
but I don't want to clutter my DAO's with Example* classes just for
PrimaryKey selection (yes, may i could use them elsewhere too...). Therefore
I will just add selectByPseudoPrimarykey() to sqlmapping xml filel (+DAO
classes) directly to skip creation of those.

Thank you for your very quick answer!
--
View this message in context: http://www.nabble.com/Abator-Primary-Key-Forcement-t1317590.html#a3515591
Sent from the iBATIS - User - Java forum at Nabble.com.


Re: Abator Primary Key Forcement

Posted by Jeff Butler <je...@gmail.com>.
Interesting idea, but I think that would be a little misleading.  Since a
view doesn't have a primary key, a "selectByPrimaryKey" method would seem
strange.

You can accomplish this function using the selectByExample method and
writing your own "pseudo primary key".  For example:

public class PseudoPrimaryKey {
  private Integer col1;
  private Integer col2;
  // getters and setters
}

In the DAO, add a method like this:

public Record selectByPseudoPrimaryKey(PseudoPrimaryKey key) {
  RecordExample example = new RecordExample();
  example.setCol1(key.getCol1());
  example.setCol1_Indicator(RecordExample.EXAMPLE_EQUALS);
  example.setCol2(key.getCol2());
  example.setCol2_Indicator(RecordExample.EXAMPLE_EQUALS);

  List result = selectByExample(example);
  If (result.size() > 0) {
    return (Record) result.get(0);
  } else {
    return null;
  }
}

Remember that any method you add to a DAO will survive a re-generation, so
you don't have to worry about adding methods to the Abator generated
classes.

I suppose we could change Abator to automatically generate methods and
objects like this, but there are a lot of things to take into consideration
(does the query return one row, or multiple rows, etc.).  I think it would
be better to leave it to the user to create these things by hand.

I wanted the selectByExample method to be very reusable - and this is a
great example of that reuse.  In my own projects, I write lots of methods
like this that reuse the selectByExample method to create custom queries.

Jeff Butler


On 3/21/06, Skrach <daniel.skrach@eunet.co.at > wrote:
>
>
> Hi,
>
> I'm currently evaluating Abator (which I'm really impressed off so far!)
> and
> came over the following limitation/idea:
>
> During creation of Maps/DAOs/Models, Primary Key classes are automatically
> created, if PK's are found on the DB-Table. For sure, this mechanism
> doesn't
> work for views (which I must use for other reasons).
>
> Therefore my question: What do you think about forced Primary Key class
> creation (for Domain, Model, Map + selectByPrimaryKey() method) specified
> as
> new element in <table ... primaryKey="a1,a2"" ... /> inside the
> abatorConfig.xml file, which take effect if no primary key is found on the
> table itself?
>
> Thanks for your help!
> --
> View this message in context:
> http://www.nabble.com/Abator-Primary-Key-Forcement-t1317590.html#a3513218
> Sent from the iBATIS - User - Java forum at Nabble.com<http://nabble.com/>
> .
>
>