我有三个班级,一个是USER,第二个是Vehicle,第三个是notfound_test
在USER类中,我将车辆声明为集合如下
收集车辆=新ArrayList();
并且还有吸气剂和固定剂。
所以,我的问题是我如何从数据库中检索数据我正在使用hibernate?
我尝试了以下代码
我无法在第三个CLASS中获取车辆JOPTIONPANE中的数据。我想要相关数据
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();
}
}
答案 0 :(得分:0)
与往常一样,您忘记了OneToMany上的mappedBy注释。它是一个双向的OneToMany关联,因此一方必须是多方的反面,如in the documentation所述:
@OneToMany(mappedBy = "user")
private Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
我还将字段重命名为vehicles
,因为它是一个集合,因此包含多个字段。
编辑:
您的评论显示的 打印了
[org.notfound.annotation.Vehicle@37ed25, org.notfound.annotation.Vehicle@aec63]
^-- first vehicle ^-- second vehicle
^-- brackets, indicating that it's a collection ------------------------------^
如果您希望打印它们的方式与dafult方式不同,则需要覆盖车辆中的toString()
方法,就像任何Java对象一样。