I have a Java class that was generated by Hibernate that represents a table
Example:
public class Customer implements java.io.Serializable {
private static final long serialVersionUID = -9144431342277803632L;
private BigDecimal customerid;
private String custcompanyname;
private String firstname;
private String lastname;
private String address;
... <getters and setters>
}
What I would like to do is to make a class that uses a subset of the attributes listed above. For example:
public class CustomerShortInfo {
private BigDecimal customerid;
private String firstname;
private String AddInNewFieldHere; << add in new field that is Not part of Customer
... <getters and setters>
}
In order to do something like this:
List<CustomerShortInfo> = List<Customer>
I am trying to keep from making a loop that goes through List in order to do an assignment manually. I have seen that one could do a "subclass" - but - as far as I can tell, this would require something like:
public class CustomerShortInfo extends Customer {
private BigDecimal customerid;
private String firstname;
... <getters and setters>
}
Again, "Customer" is the class created by Hibernate which adds/removes rows from the table. Each attribute of Customer matches a column in the database table.
Which results in the following:
List<CustomerShortInfo> shortcustomerlist;
List< ? extends Customer> longcustomerlist = shortcustomerlist;
What is needed is for the assignment to go in the opposite direction. The example below being:
List<CustomerShortInfo> = List<Customer>
Can this be done? If so, how?
TIA
Update 2
Is there any credible way to get th code below working? As of now, I am getting the following:
Exception in thread "main" java.lang.ClassCastException: ccinfw.main.test.First cannot be cast to ccinfw.main.test.Second at ccinfw.main.test.TestAssign.main(TestAssign.java:22)
public class TestAssign {
public static void main(String[] args) {
List<First> firstList = new ArrayList<>();
First firstitem = new First();
firstList.add(firstitem);
List<Second> secondList = new ArrayList<>();
secondList = (List<Second>)(List<?>) firstList;
System.out.println(" A is " + secondList.get(0).getA());
System.out.println(" B is " + secondList.get(0).getB());
System.out.println(" X is " + secondList.get(0).getX());
System.out.println(" Z is " + secondList.get(0).getZ());
}
}
class Second {
int b = 3000;
int a = 4000;
int z = 5000;
int x = 9000;
public int getA() { return a; }
public int getB() { return b; }
public int getZ() { return z; }
public int getX() { return x; }
}
class First {
int b = 30;
int a = 40;
int c = 10;
int d = 20;
int e = 80;
int f = 90;
public int getC() { return c; }
public int getD() { return d; }
public int getE() { return e; }
public int getF() { return f; }
public int getA() { return a; }
public int getB() { return b; }
}
Update 1
The use Sub Classes in this situation will not work for what I am trying to do. When using Sub Classes, all of the attributes of the Super Class (Customer)
are accessible from the Sub Class ( CustomerShortInfo )
. The attributes of CustomerShortInfo
need to stand/used alone. When it was mentioned that A was a Sub Class of B, it was just to describe how the situation.
For example, if Class A had the attributes of:
length
height
width
weight
color
manufacturer
and Class B had the attributes of weight manufacturer NumberOfTires
Note that "NumberOfTires" is not in the "Super Class"
Then, if the following line was executed:
B = copy of (A)
then I would just have
weight <<<< values comes from A
manufacturer <<<< value comes from A
NumberOfTires <<<< Value Not Set from A (since it is not there)
Why would "manufacturer" and "weight" be set when copied from A? Because they have the same attribute name. If I were given a task to "print all the attributes of B".
If I were given a task to print out ALL of the attributes of B, I would get:
weight, manufacturer, NumberOfTires
and not
ALL of the attributes of A + NumberOfTires
If such a copy is not possible, then one would have to use a number of "getters" and "setters" while looping through a list (in this case Customer) to do the job. This is what I was trying to avoid.
So, any advice on how to do this would be greatly appreciated.