I have a database table defined something like this:
table foo{
int id; // Primary key
varchar name; // Random field
int sgId; // Foreign key to another db table
…
}
My class definition looks like:
class foo{ @Id
Integer id;
String name;
@ManyToOne
@JoinColumn(name="sgid")
private Bar bar; // Instance of the object in the other db table
…
}
This all works fine until I try to load the objects into a Table.
I was trying to figure out how to add “sgid” to the ‘Visible Columns’ of my table. But I don’t see how to do it. I can generate a new column named ‘sgid’ but how do I pick up the value from the enclosed object? And do I need to make it a generated column at all in order to be able to filter on it? I don’t actually want to see the sgid value - I just want to filter the table…
So can someone help me do the following - not sure if all of these are necessary for this particular situation or not, but I’d like to know how to do all of them…
How do I create a generated column using information in an embedded object in the class?
How do I filter the table based on the sgid value in the database. I can create the filter, but I can’t seem to apply it. I tried this:
myJPAContainer.addFilter(Filters.eq(“sgid”, );
This compiles but throws an exception at runtime because of an invalid field name.
Changing “sgid” to “id” made the filter work, but that obviously isn’t what I want
Since I don’t want to actually see the sgid value in the table, do I have to create a generated column and then somehow ‘hide’ it? Can I do that? Is it necessary??
This is all being done using mysql and a JPAContainer.
Have you tried using myJPAContainer.addNestedContainerProperty(“sgid.id”)?
Filter would then be myJPAContainer.addFilter(Filters.eq(“sgid.id”, );
There’s more on this in JPAContainer’s manual, which is included in the zip.
If you want to hide columns in your table, you can use myTable.setVisibleColumns(new Object {“yourColumnName1”, …});
I have used the setVisibleColumns to display a subset of a class, but I had not figured out the Nested Container property - but I will definitely give that a try.
I’ll let you know if that solves the problem. Thanks very much!
This routine will get called every time I change the filter - is it a ‘bad’ thing to be repeatedly adding the nested container property to the JPAContainer?
Should that be done once when I create the container and left alone? Or does the removeAllContainerFilters also remove the nested container property?
I’m not sure what the ‘best practice’ should be here…
But the filter itself does work - so thanks again for the info. Most of this is just knowing what to look for - I had missed the nested property stuff when I first read through the manual, or at least I had not understood what it could be used for…
Nested container properties are not only for filtering, and it would be best to only add it once.
I don’t have the code in front of me but I believe trying to add the same property again either does nothing or replaces the property with an identical one - but it might still be suboptimal performance-wise in case extra notifications are sent to PropertySetChangeListeners.
Just to confirm: in JPAContainer, trying to add the same nested property twice does nothing, so it does not really hurt in this case.
Nevertheless, especially if using other containers, I would recommend adding it only once when setting up the container.