Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!

J Sellers

Greenhorn
+ Follow
since Mar 06, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by J Sellers

Never mind, it appears I've figured it out (at least for now)

Ganesh Shirsat wrote:Hi

you can used the discriminator concept of hibernate.. "multiple class for one table"

using discriminator you can specify the object type and accordingly that hibernate will fetch the record for your type


Thanks
Ganesh Shirsat



Ganesh, this is what I was looking for, thanks.

I do have an issue though now. I've used some of the examples I've found but they don't seem to be working. The first class will pull all the records from the table despite their "type" and the second class that extends that first class pulls nothing. Below is the code I'm using, do you see anything wrong with it?

I'm doing just a simple Hibernate createCriteria(ObjOne.class).list() to test this if that helps.





Hello all, I'm working on a new project to help me come to terms with Hibernate/JPA and had a question. I currently have ClassA which uses the "object" table:


This works fine and all is well.

My issue is that inside of this table there is a column called "type" so the rows inside of this table can actually be one of three types (A, B or C). Right now they all get lumped under ClassA. I'd like to make a Class for each "type" though, is there a way using annotations that I can kind of filter out the results of this table based on the "type" column?

So I'd like to make ClassA, ClassB, and ClassC that all use table "object" but filter based on the column "type". They'll all have the same attributes but each might have different methods associated with them because they each do different things.

I'm working with an existing database/application so I can't change the actual database layout or anything, this is basically a read only type app.

I hope that makes sense.

TIA
I have been working on creating a database backed web application as an educational project, unfortunately I've hit a snag in the design. As background, I have a "Home" and a "Region" model (classes). I also have separate DAO classes to handle interaction with the database (HomeDAO and RegionDAO). As you can probably guess, homes are organized by regions (groups of homes belong to certain regions) and now I am trying to create a method that lists each home for a certain region, something like listHomesByRegion(). My issue is I'm not sure how to create this method or where it should go. Most of my DAO code has come from this site: http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html and below is a sample of what I was planning on using inside of the RegionDAO class:


Now in theory this works fine but my issue is this: the mapHome method from above already exists in the HomeDAO class but is marked private as it's designed to only be called by HomeDAO. For that reason, I can't just import HomeDAO into RegionDAO and use that method which means I have to re-create it in my RegionDAO class which leads to code duplication and two identical methods I have to maintain (not the best solution). So what is the best way to tackle this from a design perspective? I thought about moving the entire listHomesByRegion() method into the HomeDAO class but that doesn't seem like a very good solution either.
10 years ago
Rosco, thanks for the help and advice, I appreciate it.
Rosco, I think I am following you. Using your advice in my scenario, would I actually have the sql query string (the statement and the result set) in the controller (servlet) and just have a bunch of basic getters and setters in the model class that I passed the results to?
This is where I start getting a bit confused. To me, it seems like logically you would place any code that deals with that object in its class file, whether it's a basic set/get method or a more in depth sql query. When you wanted to perform that action you would just call that objects method.

Thanks for your response, I think it cleared up a few things.
I hope this question doesn't come across dumb and that it makes sense. I am fairly new to Java and OOP in general.

As I mentioned in a previous post, I am teaching myself servlets and everything that goes with them by "porting" a PHP application to Java. I've got my initial testing servlet working with JDBC connection pooling and I've done it a few different ways, my question is which one is correct when it comes to following the MVC design pattern? Is it ok to have the database connection setup in my controller (servlet) and then passed to my methods (models) or should I have anything database related in the model class?

Here is some code on what I'm working with. I have an application specific web.xml and context.xml files that setup my database listener/connection info. This listener is called via the containers contextInitialized() method where it looks up the datasource and then makes it usable for the rest of the app:



Next, the actual servlet in this setup creates a reference to the datasource in it's init() method, creates an actual connection in it's doGet() method, and then passes that connection to my methods/models so they can connect to the database. Here is the datasource/connection setup in the servlet:


And the code from ModelClass:


This works and everything, but from what I've read regarding MVC, it sounds like I might want to move the database stuff out of the servlet and into my model classes, is that right? The other night I created a DBUtils class that handled everything and then instantiated it in the servlet init() method. I moved all the connection creation stuff outside of the servlet code and into my model code.
So I guess I have a few questions:
- Is the code up above "ok" from a design sense or does the database stuff need to be moved around?
- Is it "ok" to use contextInitialized() to setup my datasource or should I do everything from a database utility class which is instantiated via init()?
- In regards to the previous question, should I even have any database related logic in the servlet init() method or just save it all for the utility class?

I apologize for the long post, but I wanted to use some of the actual code to illustrate what I was doing.
TIA
Just one more update as it looks like I have a lot more reading to do
I think ServletContextListener is just what I was looking for as far as the database stuff goes. I'll be working more on building out the helper classes and then actually integrating them into a webapp.

Campbell Ritchie wrote:Welcome to JavaRanch

I would suggest you try sorting out your database connectivity first. I presume you have your database already set up? The Java Tutorials contains a section about database connections; have a look at that. It tells you about connecting. The MySQL manual has details about connecting; I think it is section 19. Look where it says "Java."

Do the project in little bits, and make sure each bit works before trying the next bit.



Yes, the database is all setup and populated. Thanks for the links and suggestions, I'll go over those some more (I think I've previously reviewed them).

I spent some time with google today and it looks like JDBC pooling might be what I'm looking for. This way I do not have to create a new connection every single time I want to run a query. The way the current (PHP) application is setup, basically each time the page loads it's doing an SQL query of some sort, so if I had to create the connection as well I would probably run into performance issues. It looks like the JDBC pooling will get around that for me.
I have a couple of questions that span across categories so I hope this is the right place to post this question, especially since I am a Java newbie. I'll start by saying that I am pretty new to Java having only completed an "introduction to Java" type class and am currently working my way through "Headfirst Java".

The best way for me to learn is to actually create something I want or am interested in, so for my first real project I'm trying to re-write a PHP front end application I created. Since it is the front end, all it really does is pull data from a MySQL database and display it to the user via web pages. Of course it's a bit more complex then that, but that is the high level overview.
My problem is that during my research on how to do this in Java I am getting all sorts of information that I can't really digest. Topics such as Hibernate, the Spring framework, EJB's and all other sorts of stuff start cropping up and I end up getting a bit overwhelmed with all the terms and technology. I know that this new application will need to be a servlet so I've been researching that, but outside of that I am kind of lost.

With my PHP application I use a database abstraction layer that creates a persistent connection in one .php file (the main config file). This connection is a global variable that is accessed via all the other db methods, so I can basically reference it from wherever. One thing that I am trying to wrap my head around is how I do this with Java since everything is an object? Most of the stuff I've read says to keep most of your stuff out of main() and in separate classes. My initial thought was to create a class that handled all the database stuff (connection, query, inserts, etc) but then each time I want to do something with the database I need to create a new database connection (object). That seems like it could be very messy and resource intensive. I have found a few code examples of interacting with a database, but they are all pretty basic and most of the code is run in main(). So the program works well but it only runs once and that is it.

Sorry for the rambling, I'm just looking for maybe some pointers on how to get this started and what libraries, framework(s), tools (if any) I really need to get this started. I have ordered the Headfirst Servlets & JSP book as well to help me along in this process.

TIA