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

Hibernate Return a List of countries from a DB

 
Ally Cavs
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I want to return a list of countries from a table.
The table is simply called Countries. has one column called "country"
This column lists countries

How do I write a Hibernate XML file to map this column to java.util.List
I cant figure it out

A bag will do, I think for a <list> i need an extra column for indexing.
I dont mind if the result set doesnt come back in the same order because I can just do a sort on the collection
Or to an order by in the query actually.

Thanks
Ally
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't the table have a primary key? All database tables should have a primary key, especially if you want to map the table to a Java object. Hibernate needs to have a way to say This Java Object maps to one and only one row in the database, and only a primary key can do that for you.

So let's say you have two records in the database table that country = "England", and you have a Country object where the attribute is "England", which one of the two records does that Object represent. It can't represent both records.

Mark
 
Ally Cavs
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

I understand what you are saying

Ok so my table (country ) is

country_name (field name and primary key)
-------
England
Scotland
Wales
N. Irl
etc


This table will be used to populate a drop down list

so my mappings are so

where DropDownLists
is




But this does not work as for each row I get back an instance of DropDownLists
I think the the collection elements such as <bag> or <set> is the solution but I cant get them to work either.

This seems very simple and straight forward problem yet I can not figure out a solution

Thanks
Ally
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly. It is working like it should based on your mapping.

I would think you would have a Country class

public class Country {

private int id;
private String name;

}

Then you map the Country class to your table.

You do not map a class that is holding Collections of Country objects.

You can do a query after mapping get a List of Country objects, then put that collection into your DropDownLists object if you want.

Mark
 
Ally Cavs
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SO you are telling me I can only map a POJO to a table
I couldnt map java.util.String

There is only one field in the table so I wont have an ID attribute in my POJO just a name

I find it very strange that you cant simply get a list of strings if querying a table that only has one column?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ally Cavs wrote:SO you are telling me I can only map a POJO to a table
I couldnt map java.util.String

There is only one field in the table so I wont have an ID attribute in my POJO just a name

I find it very strange that you cant simply get a list of strings if querying a table that only has one column?


Well it leads me to my first post. Hibernate must have an ID to map to so that Hibernate can distinguish what row in the database table the Object represents. So your table must have a Primary Key to do the ID mapping. It very well could be your one field. Then you would have (I am using Annotations to be quick here, sorry)

@Entity
public class Country {

@Id
String name;
}


Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic