473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Generics problem accessing static field

So here is my problem. I have two beans (one extends the other), and I have one DAO class for each to access their tables (using Hibernate). In most cases the queries will be identical so I also have an abstract DAO class (extended by the other two DAOs) which contains the majority of the queries. I had the methods returning beans of the super class which, if need be, I would cast to the subclass. To avoid this casting I want to switch to generics. I changed over the abstract class with no compiliation erros, but I've hit a bump. In the two beans I have a static String field "entityName" which I access to for the Hibernate calls (Bean.entityName). When I converted to generics the reference changed to T.entityName which now always references the super class. What I need is to access whichever class' entityName I happen to be using.
Example:
Expand|Select|Wrap|Line Numbers
  1. class AbstractBeanDAO <T extends SuperBean> {
  2.  
  3.  public T get(Integer id){
  4.   // this where I have the problem
  5.   Criteria criteria = session.createCriteria(T.entityName);
  6.   // set some criteria
  7.   return criteria.uniqueResult();
  8.  }
  9. }
  10.  
No matter what I set as AbstractBeanDAO <type> it always references SuperBean.entityName. There are times I need SubBean.entityName so I can access the correct table. How can I do this without having to duplicate code in the two DAO subclasses?
Jan 14 '09 #1
2 3594
JosAH
11,448 Expert 8TB
Build one small non-static method in every class that returns that particular value. If the type of that value differs per class make that method return it as an Object.

kind regards,

Jos
Jan 14 '09 #2
I've been thinking about this, and I've come roughly the same conclusion (not yet implemented). I can't just make a non-static method in my beans because I can't make a static call to it. I need to add an abstract getEntityName to the abstract dao, and implement them in the concrete classes. The concrete classes will always use a same (non-generic) bean type so this shouldn't be an issue. Now in the abstract calls wherever I make the T.entityName call I would replace that with this.getEntityName();
Jan 14 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Learning C# | last post by:
I hope this is an ok place to post real beginner stuff. Basically I need to know the difference between a field, and a static field. My book sux and doesn't explain this well. It seems that a...
6
by: Carlos | last post by:
Hi all, I am trying to access a public field of another form class within the same namespace. The field is public, what is the best way to access it from a different class? I defined as private...
3
by: Alexander | last post by:
Hi all I has Component inside ASP.NET page that create CO object and store reference to it in the Component's privat static field. Then I cast this reference to desired interfac and call...
4
by: abCSharp | last post by:
I understand that static variables have app-domain scope. Till the app-domain is in memory, the static variable will be in memory. When are the app-domains unloaded is the question. I have read...
4
by: taub.adam | last post by:
Hi, Can generics be used to call a static method of a class? The below example gives an error in my generictest class. Anybody know why? class foo { static public void Test() {...
5
by: feel_energetic | last post by:
Hi, I already knew how to define a static method of a class( using staticmethod() ),but I find there isn't a built-in func to build a static field ( something like staticfield() ) can anyone...
8
by: munk | last post by:
I post this a C# newbie - please bear with me :-) I have coded a small project in C#, and ran into a problem illustrated in the below minimal project cs file. I wonder how to make the following...
1
by: Rach26 | last post by:
Hello I am currently developing my masters project and am relativly new to C# I am having a problem with my Cache. In settings.settings I have placed the following after my connection setting: ...
8
by: mrcw | last post by:
Hi I've added a textbox called tbAccel0 to a form. It shows up on the form with the correct name, but when I look at the code a red squigly line appears under tbAccel0 and an error appears in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.