Andy Westley

Greenhorn
+ Follow
since Nov 09, 2004
Merit badge: grant badges
For More
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 Andy Westley

Hi,

I'm pretty new to hibernate and it feels like I'm blagging my way through it. I thought I'd been stumbling in the right direction until I hit my latest problem. I'm trying to build a social networking app - you know users, friend invites, friendships, etc. I've always done my own DAOs and standard JDBC code in the past, but this felt complicated enough to warrant some ORM, but learning it as I'm going along is leaving me feeling a bit confused.

Basically I think my problems revolve around mapping collections. Building from scratch I would have assumed I would use ArrayLists as my properties, having followed a load of tutorials I've sort of ended up with Bags, I think it's causing a problem. I've included some code below - which I've simplified from my actual source just to save a bit of space.

User.hbm.xml

Invite.hbm.xml

As these stand, I can store a new invite, but I had assumed that the columns flagged as being list-index properties would be set automagically for me. Instead it's null, and when I try to return a list of invites for a user I get the following stack trace:

As I said, it's clear the list-index field is null which causes the error, but how do I make sure that's set? Is it automatic or do I need to do something?

I've switched away from the bags in the mapping config because I was beginning to run into errors where returning what I thought would be a Collection:


Is there enough in there to explain my problem? I think ultimately I want to be using List as my collection type but don't fully understand how and when the list-index field gets populated. I've got the caveat-emptor code and seen it too uses List in the item class, but I can't seem to trace how it gets used.

Any help would be gratefully received. I've come to far to think about rewriting this, but seem to have hit a brick wall. I can't seem to find any detailed examples of this working online, but there does seem to be quite a lot people struggling with it.

Cheers
aw


One solution I guess is to not hold the user object in my HttpSession, but some other sort of ctx object that I create based on the user object. That way when I need to get the activities I need to get the user again, but have the id readily available.

I'm sure that would work, but is it the right way to do thing?
Hi there,

This might be a really stupid questions, but its getting late here and I'm probably missing something obvious. I'm pretty new to hibernate, but the speed with which I've got my new webapp going is frightening - maybe that's the problem - I'm skipping along without getting all the details.

I'm writing a fairly simple MVC webapp. User logs in, user object retrieved from database and stored in HttpSession object. First page of app displays a list of activities stored in user object contains list of activities.

Functionality I've written currently happily adds a new activity directly to the database with a foreign key back to the logged in user.

However whenever I access the user object from HttpSession object, it's collection of activities never includes newly added activities. If I restart the app, or kill my session and log in again, the user object happily lists all the original + new activities.

Am I missing something hibernatey, or is the a fatal flaw in the design of my app?

I'm not actively putting the activity object into the collection of activities in the existing user object and then saving that. Instead, I'm creating a new activity, and adding the user object to it. Is that my problem? Should I be updating the user, rather than directly adding the activity?

After a quick google, I tried adding session.refresh to the end of the saveOrUpdate method of my GenericDAO, but that's done nothing.

Any help for a frazzled brain?
Hi there, it turns out that if I specify the class of my many-to-one it all generates OK



Still happily using *.hbm.xml in my hibernatetool target.
Sorry, I tried the following and I get the same error:




Should I be adding a classpath somewhere so that I hbm2java knows where to look for references to othe generated classes it might need? None of the other examples I've seen do aything other thean *.hbm.xml. Surely I'm not doing anything that radical?!?
[ October 20, 2008: Message edited by: Andy Westley ]
Thanks for the speedy response.

Is changing the order a simple question of explicitly referencing the mapping files one by one in my ant fileset? At the moment I just include *.hbm.xml. It would be great if it could resolve those kind of dependencies.
[ October 20, 2008: Message edited by: Andy Westley ]
Hi there, I'm trying to get up to speed with hibernate and am following the basic tutorial, the tools tutorial, whilst trying use it on a project I'm working on. I'm trying to set up a bidirectional one-many relationship between a customer and a job. A customer will have many jobs, but job only have one customer. From every customer I want to be able to get to their job objects, and from every job I want access to the customer object. I'm using the hibernatetool ant task to generate class source code based on my mapping files:


This is a snippet from my ant target



When I run that target, I get the following output

[hibernatetool] Executing Hibernate Tool with a Standard Configuration
[hibernatetool] 1. task: hbm2java (Generates a set of .java files)
[hibernatetool] log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
[hibernatetool] log4j:WARN Please initialize the log4j system properly.
[hibernatetool] An exception occurred while running exporter #2:hbm2java (Generates a set of .java files)
[hibernatetool] To get the full stack trace run ant with -verbose
[hibernatetool] Failed in building configuration when adding /Users/andrew/Documents/workspace/JobRecordKeeper/conf/hibernate/data/Job.hbm.xml
[hibernatetool] org.hibernate.InvalidMappingException: Could not parse mapping document from file /Users/andrew/Documents/workspace/JobRecordKeeper/conf/hibernate/data/Job.hbm.xml
[hibernatetool] org.hibernate.MappingException: class uk.co.blueballoon.jobrecordkeeper.core.Job not found while looking for property: customer
[hibernatetool] java.lang.ClassNotFoundException: uk.co.blueballoon.jobrecordkeeper.core.Job
[hibernatetool] A class were not found in the classpath of the Ant task.
[hibernatetool] Ensure that the classpath contains the classes needed for Hibernate and your code are in the classpath.

BUILD FAILED
/Users/andrew/Documents/workspace/JobRecordKeeper/build.xml:155: Failed in building configuration when adding /Users/andrew/Documents/workspace/JobRecordKeeper/conf/hibernate/data/Job.hbm.xml

My guess is that it's telling me there is a problem which I can only describe as a circular dependency. When generating the code for the Job class, the task references the Customer class, which in turn references the Job class which is "under construction".

Is my understanding correct?

If it is, how do I solve the problem? I'm guessing that I'm complicating things by trying to generate my code rather than write it all from scratch - I know I'm running before I can walk, but the code generation seems to be a big advantage.

Can anyone shed any light for me?
Hi all,

I've just been knocking up little application in eclipse and I'm messing about with the jarbundler ant to build a .app application bundle. When I've been writing and running the application in eclipse I've been using log4j to write out a logfile and some standard io code to load a properties file via an argument on the command line. However, I'd like log4j to write its logfile inside the application bundle and I'd like to load the properties file in from inside the bundle, and I'm not sure how to reference files within the bundle. Do I just treat it like the folder it is behind the scenes?
15 years ago
Thanks Paul, that was a good tip. Unfortunately, it threw my theory out the window. Checking the URL in the way you suggested proves that I'm parsing the updated file from the filesystem everytime I access it.

That leaves the question of why the

XDocument.Factory.parse(myInputStream)

call doesn't seem to get the up-to-date contents of the file.

I've got a work around, but it's a bit that it doesn't work as expected.

Cheers
aw
Hi there,

I've posted this in the xml forum, because I'm having trouble with some xml/xmlbeans, but I'm not convinced it's not a classloader issue.

I'm using an xml file as a simple data store for a web app I'm writing. I'm deploying the webapp including a default xml file for start up, but add, edit and delete operations throught we webapp will be modifying the xml file that gets exploded out of the .war file.

For neatness I've been using the code

MyUtitlityClass.class.getResourceAsStream("/my_data_file.xml")

to load the file from what I thought would be the file system. As you might be able to detect I've been having problems where, saving the file updates the file on the file system (I can see it update in my IDE), but the next time I load the file, my updates don't appear. I'm wondering whether getResourceAsStream is returning me a reference to the file in the .war, rather than the exploded and updated one on the filesystem.

The only thing that makes me think otherwise, is that if I restart tomcat, the application loads the updated version of the file. It's like the reference to the file is cached - obviously without me intending it to be.

Does anyone recognise the symptoms? Can anyone suggest any approaches for identifying whether the problem is in the loading or in the parsing?

Cheers
aw
Me again,

Here's the servlet mapping bit of my web.xml

<servlet-mapping>
<servlet-name>ConfigServlet</servlet-name>
<url-pattern>/config.servlet</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/controller.servlet</url-pattern>
</servlet-mapping>

I did kill all my sessions and try again, but with my security stuff as below:

<security-constraint>
<web-resource-collection>
<web-resource-name>Secure bits of webchecker</web-resource-name>
<description>pages which require login</description>

<url-pattern>/controller.servlet?action=PrepareAddFormAction</url-pattern>
<!--
<url-pattern>/controller.servlet?action=PrepareAddFormAction</url-pattern>
<url-pattern>/controller.servlet?action=PrepareEditFormAction</url-pattern>
<url-pattern>/controller.servlet?action=PrepareDeleteFormAction</url-pattern>
<url-pattern>/controller.servlet?action=AddJobAction</url-pattern>
<url-pattern>/controller.servlet?action=EditJobAction</url-pattern>
<url-pattern>/controller.servlet?action=DeleteJobAction</url-pattern>
-->
</web-resource-collection>
<auth-constraint>
<description>Must authenticate before querying the system</description>
<role-name>webchecker-admin</role-name>
</auth-constraint>
</security-constraint>

I seem to have unrestricted access to the PrepareAddFormAction.

I thought this was the simple way to do it. I'm in danger of it being quicker to write a filter and load security details manually!

Thanks for the help though, chaps. Any other ideas?
16 years ago
Haven't ever bothered to use tomcat authentication before, but am having trouble configuring my web.xml. I've included the relevant bits of my web.xml below.

<security-constraint>
<web-resource-collection>
<web-resource-name>Secure bits of webchecker</web-resource-name>
<description>pages which require login</description>

<url-pattern>/*</url-pattern>
<!--
<url-pattern>/controller.servlet</url-pattern>
<url-pattern>/controller.servlet?action=SomeSecureActivity</url-pattern>
-->
</web-resource-collection>
<auth-constraint>
<description>Must authenticate before querying the system</description>
<role-name>webchecker-admin</role-name>
</auth-constraint>
</security-constraint>

<login-config>
<auth-method>BASIC</auth-method>
<realm-name>default</realm-name>
</login-config>

<security-role>
<description>Any user of the system</description>
<role-name>webchecker-admin</role-name>
</security-role>

I think I've managed to secure everything using a url-pattern of /* (I get challenged for password).

But when I try to go a bit further and use /controller.servlet as a url-pattern I seem to be able to get to the servlet unchallenged.

More specifically, because I'd like to restrict access to particular actions, if I use the url-pattern of /controller.servlet?action=SomeSecureActivity I get through unchallenged again.

Can anyone tell me whether I should be able to use a servlet name and even bits of the querystring?

Cheers
aw
16 years ago
Bit of a brain burp, including the catalog_item table twice in there...

SELECT `catalog_item`.`id`,
`catalog_item`.`name`,
`spied`.`spied`
FROM `catalog`,
`catalog_item`
LEFT JOIN (`spied`, `user`) ON ((`spied`.`catalog_item_id` = `catalog_item`.`id`) AND (`spied`.`user_id` = `user`.`id`) AND (`user`.`id` = 3))

WHERE `catalog_item`.`catalog_id` = `catalog`.`id`
AND `catalog`.`id` = 1
ORDER BY `catalog_item`.`name`

works fine.
I've had another go:

SELECT`catalog_item`.`id`,
`catalog_item`.`name`,
`spied`.`spied`
FROM`catalog`,
`catalog_item`
LEFT JOIN (`spied`, `catalog_item` as catalog_item2, `user`) ON ((`spied`.`catalog_item_id` = `catalog_item2`.`id`) AND (`spied`.`user_id` = `user`.`id`) AND (`user`.`id` = 3))

WHERE`catalog_item`.`catalog_id` = `catalog`.`id`
AND`catalog`.`id` = 1
ORDER BY `catalog_item`.`name`

This gets me a bit closer, but the results are still not quite there:

"3";"Barn owl";"2007-05-01 09:27:49"
"3";"Barn owl";"2007-05-06 12:30:30"
"1";"Oyster catcher";"2007-05-01 09:27:49"
"1";"Oyster catcher";"2007-05-06 12:30:30"
"2";"Ringed plover";"2007-05-01 09:27:49"
"2";"Ringed plover";"2007-05-06 12:30:30"
"4";"Tawny owl";"2007-05-01 09:27:49"
"4";"Tawny owl";"2007-05-06 12:30:30"

The actual data says that Oyster catcher was spotted at 0927 and Barn owl was spotted at 1230.

Does my valiant attempt inspire anyone with some tips?
Hi there,

I'm working on an application that will register when users see particular things. Some of the database is outlined below:

User: id, name
Catalog: id, name
CatalogItem: id, name, catalog_id
Spotted: user_id, catalog_item_id, spotted_date

So, a Catalog is a collection of CatalogItems (eg, Planes would contain 747, 767, DC10, etc). Spotted registers when an individual user has seen one of the CatalogItems. What I was hoping to do was put together a query that returns me a list of all the CatalogItems with the date spotted, if it had been. The closest I've got is the following:

(SELECT `catalog_item`.`id`, `catalog_item`.`name`, `spied`.`spied` FROM `user`, `catalog`, `catalog_item`, `spied` WHERE`user`.`id` = 3 AND `spied`.`user_id` = `user`.`id` AND `spied`.`catalog_item_id` = `catalog_item`.`id` AND `catalog_item`.`catalog_id` = `catalog`.`id` AND`catalog`.`id` = 1)
UNION
(SELECT `catalog_item`.`id`, `catalog_item`.`name`, 'xxx' as spied FROM `catalog`, `catalog_item` WHERE`catalog_item`.`catalog_id` = `catalog`.`id` AND `catalog`.`id` = 1)
ORDER BY `name`

That seems a bit heavy handed and would still leave me having to filter out the duplicate (items that have been spotted from the second sub-query) in some code.

It feels like an outer join should be the order of the day, but I've tried and I can't write one correctly that (left?) outer joins CatalogItem and Spotted, and maintains the rest of the conditions. Everything I've tried is syntactically wrong, rather than returning wrong data. Can anyone point me in the right direction? I'm using MySql if that makes a difference.

Outer joins have always confused me a bit!

Cheers
[ May 07, 2007: Message edited by: Andy Westley ]