Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hbm2java - private members ?

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to generate private setters/getters for my properties via the hbm2java tool ?

Thanks

Pho
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure for my answer.

access attribute of property tag



The access attribute lets you control how Hibernate will access the property at runtime. By default, Hibernate will call the property get/set pair. If you specify access="field", Hibernate will bypass the get/set pair and access the field directly, using reflection. You may specify your own strategy for property access by naming a class that implements the interface net.sf.hibernate.property.PropertyAccessor.

 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is hbm2java ?

Is it a tool to generate java classes?
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to generate private setters/getters for my properties


What is use of private setters/getters ? Who will use it?
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradeep Bhat:
What is hbm2java ?

Is it a tool to generate java classes?


Yes. of course.


Hibernate Extensions. This package includes tools for the following tasks:

Java source generation from a mapping file (aka CodeGenerator, hbm2java)

mapping file generation from compiled Java classes or from Java source with XDoclet markup (aka MapGenerator, class2hbm)

There's actually another utitily living in Hibernate Extensions: ddl2hbm. It is considered deprecated and will no longer be maintained, Middlegen does a better job for the same task.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somkiat,

The "access" attribute is to tell Hibernate what PropertyAccessors to use.
hbm2java seems to be able to generate only public setters/getters. I want
these to be private.

Pho
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pho Tek:
Somkiat,

The "access" attribute is to tell Hibernate what PropertyAccessors to use.
hbm2java seems to be able to generate only public setters/getters. I want
these to be private.

Pho


Why you want to do this?
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pho Tek:
Is it possible to generate private setters/getters for my properties via the hbm2java tool ?

Thanks

Pho


Yes it is possible using a meta element inside your property. The meta should look as:
<meta attribute="scope-get|scope-set|scope-field">private</meta>.

./pope

PS: I don't know how is Hibernate working after this. But I don't thing it is a good decision to use private getters/setters as their scope is to permit access to the property not to be internally used by the class.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I don't thing it is a good decision to use private getters/setters as their scope is to permit access to the property not to be internally used by the class.


If you are using internally within a class then why dont you access fields directly.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pradeep Bhat:


If you are using internally within a class then why dont you access fields directly.


I agree with you...
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want disallow the user of the class from setting nulls to some required properties.

Thus, if I have a mapped property "privateFoo", then I will codegen:

[ September 03, 2004: Message edited by: Pho Tek ]
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So in fact need some more code generated on your source. The solution you want is not reachable. (for the same field generate 2 methods).

You can do the following trick. Generate the Hibernate POJOs with their natural getters/setters and then extend there overridding the settters.

./pope
 
Max Andersen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

PS: I don't know how is Hibernate working after this. But I don't thing it is a good decision to use private getters/setters as their scope is to permit access to the property not to be internally used by the class.[/QB]


Hibernate does not "care". If the properties are private it uses reflection-tricks to access the private methods (this of course relies on the right security policies - but in a default Java VM you will not have problems)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic