简体   繁体   中英

retrieve data of collection type from database using getters and setters

i am having three classes one is USER, second Vehicle and third is notfound_test.
In USER class i declared vehicle as Collection as follow.
Collection vehicle=new ArrayList();
And also having getters and setters of it.
So, my question is how i can retrieve data from database i am using hibernate?
I tried following code.
i am unable from getting data in JOPTIONPANE OF VEHICLE IN THIRD CLASS.i wanted the associated data

package org.notfound.annotation;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class User {

@Id @GeneratedValue
private int id;
private String name;
@OneToMany
Collection<Vehicle> vehicle=new ArrayList<Vehicle>();


public Collection<Vehicle> getVehicle() {
    return vehicle;
}
public void setVehicle(Collection<Vehicle> vehicle) {
    this.vehicle = vehicle;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}


}

package org.notfound.annotation;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Vehicle {

@Id @GeneratedValue
private int vehicle_id;
private String vehicle_name;
@ManyToOne
private User user=new User();



public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}
public int getVehicle_id() {
    return vehicle_id;
}
public void setVehicle_id(int vehicle_id) {
    this.vehicle_id = vehicle_id;
}
public String getVehicle_name() {
    return vehicle_name;
}
public void setVehicle_name(String vehicle_name) {
    this.vehicle_name = vehicle_name;
}


}

 package org.notfound.annotation;

 import javax.swing.JOptionPane;

 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;

 public class Notfound_test {


public static void main(String[] args) {

    User user=new User();
    Vehicle vehicle=new Vehicle();
    Vehicle vehicle2=new Vehicle();

    user.setName("NIRAV J. KAMANI");
    vehicle.setVehicle_name("CHEVROLET CRUIZE");
    vehicle2.setVehicle_name("MARUTI ERTIGA");
    vehicle.setUser(user);
    vehicle2.setUser(user);
    user.getVehicle().add(vehicle);
    user.getVehicle().add(vehicle2);

    SessionFactory sessionfactory=new Configuration().configure().buildSessionFactory();
    Session session=sessionfactory.openSession();
    /*session.beginTransaction();
    session.save(user);
    session.save(vehicle);
    session.save(vehicle2);
    session.getTransaction().commit();
    session.close();*/

    session.beginTransaction();
    user=(User) session.get(User.class, 1);
    vehicle=(Vehicle) session.get(Vehicle.class, 1);
    vehicle2=(Vehicle) session.get(Vehicle.class, 2);
    JOptionPane.showMessageDialog(null, user.getId()+" "+user.getName()+" "+user.getVehicle().toString(), "INFORMATION", JOptionPane.INFORMATION_MESSAGE);
    session.close();

}

}

As always, you forgot the mappedBy annotation on the OneToMany. It's a bidirectional OneToMany association, and the one side must thus be the inverse side of the many side, as explained in the documentation :

@OneToMany(mappedBy = "user")
private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();

I also renamed the fields to vehicles , since it's a collection, and thus contains several ones.

EDIT:

Your comment shows that the 2 vehicles are printed:

[org.notfound.annotation.Vehicle@37ed25, org.notfound.annotation.Vehicle@aec63]
 ^-- first vehicle                       ^-- second vehicle
^-- brackets, indicating that it's a collection ------------------------------^

If you want them to be printed is a different way than the dafult way, you need to override the toString() method in vehicle, as for any Java object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM