Z.H
Z.H

Reputation: 25

Moving list of item of different objects

Hi guys, I am a real noob and a layman. I need help on how to copy items from booking to bookinghist. Say that I have the Java class with partially different fields. How can this be done?

List<Booking> booking= new ArrayList<Booking>();
List<BookingHist> bookinghist = new ArrayList<BookingHist>();

This is the Booking class below. Here, the only different fields between the two classes is that BookingHist has a "extras" column.

public class Booking implements Serializable {

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "dt")
        private Date dt = new Date();
        @Column(name = "bookingname")
        private String bookingname;

        public Booking(Date dt, String bookingname) {

            this.dt=dt;
            this.bookingname=bookingname;              
        }

        public Date getDt() {
            return Dt;
        }

        public void setDt(Date Dt) {
            this.Dt = Dt;
        }

        public String getBookingname() {
            return Bookingname;
        }

        public void setBookingname(String Bookingname) {
            this.Bookingname = Bookingname;
        }
    }

This is the BookingHist class.

public class BookingHist implements Serializable {

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "dt")
        private Date dt = new Date();
        @Column(name = "bookingname")
        private String bookingname;
        @Column(name = "createdDt")
        private Date createdDt;

        public BookingHist(Date dt, String bookingname, Date createdDt) {

            this.dt=dt;
            this.bookingname=bookingname;
            this.createdDt=createdDt;
        }

        public Date getDt() {
            return Dt;
        }

        public void setDt(Date Dt) {
            this.Dt = Dt;
        }

        public String getBookingname() {
            return Bookingname;
        }

        public void setBookingname(String Bookingname) {
            this.Bookingname = Bookingname;
        }

        public Date getCreatedDt() {
            return createdDt;
        }

        public void setCreatedDt(Date createdDt) {
            this.createdDt = createdDt;
        }

    }

Upvotes: 1

Views: 192

Answers (2)

Tea
Tea

Reputation: 912

You can consider using MapStruct MapStruct

Check out an example Example map Entity to Dto

Upvotes: 0

Ryuzaki L
Ryuzaki L

Reputation: 40078

If you have the all args constructor in BookingHist like below

public BookingHist(Date dt, String bookingname, String extras) {

     this.dt=dt;
     this.bookingname=bookingname;
     this.extras=extras;
 }

Then you can convert Booking to BookingHist by using stream

List<BookingHist> bookinghist = booking.stream()
                                       .map(b-> new BookingHist(b.getDate(), b.getBookingname(), "extra value");
                                       .collect(Collectors.toList());

Upvotes: 1

Related Questions