简体   繁体   中英

how to save values from one class object to another class object

I have two classes(Pojos) name Soldbookdetails.java and Bookdetails.java , what I want to do is, I have to get data from Bookdetails table( Soldbookdetails.java ) and save the same data to my Soldbookdetails table( Soldbookdetails.java ).

ActionClass.java

private Double[] id;//With getter and setter
private Double[] quantity;  //With getter and setter
Bookdetails book=new Bookdetails();//pojos,//With getter and setter
Soldbookdetails sbook=new Soldbookdetails();//pojos,//With getter and setter 
BookdetailsDAO dao=new BookdetailsDAO(); 
SoldBooksTransactionDAO dao2=new SoldBooksTransactionDAO(); 
------------

(Note: my both pojo are same only their Class name are different )

My problem : I am unable to save record from Bookdetails.java to Soldbookdetails.java .(See My above ActionClass.java class,inside the execute method , i have mention the ERRROR).

After getting record by Bookid , i am unable to save the record into my Soldbookdetails.java .

Please help me to solve my problem.

Your saveSoldbooks(Soldbookdetails s) method takes an object of Soldbookdetails as argument but you are passing an object of class Bookdetails .

The thing you can do is you can add a method that copies the attributes of an Bookdetails object to an Soldbookdetails object (without the id field). Then you should try to save the object of Soldbookdetails using your existing method.

While keeping sold books and books in separated tables is (IMO) good perfomance issue, I think you don't need to create separated IDENTICAL classes for them. Consider making you're class BookDetails an abstract and annotating it as MappedSuperclass :

@MappedSuperclass
public abstract class BookDetails{
    // fields
    // getters
    // setters
}

@Entity
@Table(name="SoldBooks")
public class SoldBookDetails extends BookDetails{
    // Currently blank
}


@Entity
@Table(name="BookDetails")
public class TradingBookDetails extends BookDetails{
    // Currently blank
}

While currently you're classes are identical, at future you may decide to create others book types (like other table and classes for purchasing books to store or books in the way to customer) and, of course, there may appear different logic for SoldBooks and others, and in this case such 'abstraction' will simplify maintenance.

The only changes to you're saveSoldBook I currently see is to change it's argument type or provide type check in the body of the method, as long as while argument type is BookDetails, you can pass in the function any variable, derived from it - therefore, TraidingBookDetails, SoldBookDetails etc. (Changes needed depends on you're logic in this method and DAO itself).

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