HI
i have a entity class vehicle and a entity class Customer. A customer has many vehicles, so in the vehicle entity class is as below

@Entity
@Table(name = "VEHICLE")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Vehicle.findAll", query = "SELECT v FROM Vehicle v"),
        @NamedQuery(name = "Vehicle.findByMiles", query = "SELECT v FROM Vehicle v WHERE v.miles = :miles")})
public class Vehicle implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "VEHICLEID")
    private Integer vehicleid;
    @Size(max = 50)
    @Column(name = "MODEL")
    private String model;
    @Size(max = 50)
    @Column(name = "MAKE")
    private String make;
    @Column(name = "VEHICLEYEAR")
    private Integer vehicleyear;
    @Size(max = 50)
    @Column(name = "REGNO")
    private String regno;
    @Size(max = 50)
    @Column(name = "VIN")
    private String vin;
    @Column(name = "MILES")
    private Integer miles;
    @OneToMany(mappedBy = "vehicleid")
    private Collection<Appointment> appointmentCollection;
    @JoinColumn(name = "CUSID", referencedColumnName = "CUSID")
    @ManyToOne
    private Customer cusid;

    public Vehicle() {
    }

    public Vehicle(Integer vehicleid) {
        this.vehicleid = vehicleid;
    }

    public Integer getVehicleid() {
        return vehicleid;
    }

    public void setVehicleid(Integer vehicleid) {
        this.vehicleid = vehicleid;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public Integer getVehicleyear() {
        return vehicleyear;
    }

    public void setVehicleyear(Integer vehicleyear) {
        this.vehicleyear = vehicleyear;
    }

    public String getRegno() {
        return regno;
    }

    public void setRegno(String regno) {
        this.regno = regno;
    }

    public String getVin() {
        return vin;
    }

    public void setVin(String vin) {
        this.vin = vin;
    }

    public Integer getMiles() {
        return miles;
    }

    public void setMiles(Integer miles) {
        this.miles = miles;
    }

    @XmlTransient
    public Collection<Appointment> getAppointmentCollection() {
        return appointmentCollection;
    }

    public void setAppointmentCollection(Collection<Appointment> appointmentCollection) {
        this.appointmentCollection = appointmentCollection;
    }

    public Customer getCusid() {
        return cusid;
    }

    public void setCusid(Customer cusid) {
        this.cusid = cusid;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (vehicleid != null ? vehicleid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Vehicle)) {
            return false;
        }
        Vehicle other = (Vehicle) object;
        if ((this.vehicleid == null && other.vehicleid != null) || (this.vehicleid != null && !this.vehicleid.equals(other.vehicleid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.mycompany.Vehicle[ vehicleid=" + vehicleid + " ]";
    }

}

when i add a vehicle record, i have to add the customer id to the vehicle record

 @PersistenceContext(unitName = "autodealerPU")
    private EntityManager entityManager;

    @Override
    public void addVehicle(int id, String make, String model, int year, String reg, String vin, int miles,int cid) {

        Vehicle v = new Vehicle();
        v.setVehicleid(id);
        v.setMake(make);
        v.setModel(model);
        v.setVehicleyear(year);
        v.setRegno(reg);
        v.setVin(vin);
        v.setMiles(miles);
       v.setCustID(cid); //get an error because cid should be Customer type in the vehicles entity class
        entityManagerpersist(v);
    }

so how do i get the relevant customer to add to the vehicle record?

appreciate a reply

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.