Write a SQL to Join tables and save it to a List <Object>
I am using Netbeans 6.9.1, glassfish 3.1, and MySQL as my DB.
I have a List <Hotel>
object, that saves all the records from the Hotel Table (from MySQL) into it.
There is another object called List <HotelVacancy>
, which too reads from the HotelVacancy Table from MySQL and stores the records in it.
Now what i want to 开发者_JAVA技巧do is, to get some Columns from the Hotel
table and HotelVacancy
table and save it in a List <??????>
object (i have put ????
in the list
as i am not sure what its type should be). What should be the type here ? and How do i write an SQL for this ?
I don't know what exactly you are trying to achieve, but this looks like a simple 1:n relation.
If that's the case, a hotel has multiple vacancies, so the HotelVacancy class then holds an instance to Hotel.
your list would then contain HotelVacancies you can get the Hotel instance then via a getter (getHotel() or something like that).
It will be a bit tricky to build such an object model just from acessing the database, that's why you would usually use a orm (object relational mapping) framework.
Common frameworks for java are: hibernate, eclipselink, or JPA
I'm sure Google will give you quite a few tutorials with these hints
I would try creating your own class containing the columns from HotelVacancy and Hotel that you want to use and using that for the type of List<?????>
.
精彩评论