I have this class:
public class Person
{
/**
*
*/
private static final long serialVersionUID = 1L;
private String firstName = "Vasya";
private String lastName = "Pupkin";
private Integer age = 58;
private Integer phone = 2;
#Override
public String toString()
{
return "Person [firstName=" + firstName + ", lastName=" + lastName
+ ", age=" + age + "]";
}
public void setName(String name)
{
firstName = name;
}
public void setLastName(String lName)
{
lastName = lName;
}
public void setAge(Integer personAge)
{
age = personAge;
}
public void setPhone(Integer personPhone)
{
phone = personPhone;
}
public String getName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public Integer getAge()
{
return age;
}
public Integer getPhone()
{
return phone;
}
public void Init()
{
this.setName("");
this.setLastName("");
this.setPhone(0);
this.setAge(0);
}
}
I create an variable: Person somePerson, then I call method setName from that variable somePerson:
somePerson.setName("");
but it raises an error.
Based on the provided code, the following should work:
Person somePerson = new Person();
somePerson.setName("");
If it doesn't, then something else is going on.
Related
in the line of books.add it says incompatible types: string cannot be converted to Author
I tried converting it all to string but it still didn't work. My problem is how do i make the string be converted to author. The expected output of this is to simply create an arraylist of 5 books with the class of authors, books, and publishing dat. Under the Book class, i am trying to add the arraylist but i cannot seem to get pass the problem of the string to be converting to the object author.
public class Book {
private String bookTitle;
private Author name;
private PubDate date;
public Book() {
}
public Book(String bookTitle, Author name, PubDate date) {
this.bookTitle = bookTitle;
this.name = name;
this.date = date;
}
public String getBookTitle() {
return bookTitle;
}
public Author getName() {
return name;
}
public PubDate getDate() {
return date;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
public void setName(Author name) {
this.name = name;
}
public void setDate(PubDate date) {
this.date = date;
}
#Override
public String toString() {
return "Book{" + "bookTitle=" + bookTitle + ", name=" + name + ", date=" + date + '}';
}
}
public class Author {
private String firstname;
private String lastname;
private String title;
public Author() {
}
public Author(String firstname, String lastname, String title) {
this.firstname = firstname;
this.lastname = lastname;
this.title = title;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public String getTitle() {
return title;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public void setTitle(String title) {
this.title = title;
}
#Override
public String toString() {
return "Author{" + "firstname=" + firstname + ", lastname=" + lastname + ", title=" + title + '}';
}
}
public class Serialization {
public static void main(String[] args) {
ArrayList<Book> books = new ArrayList<Book>();
ArrayList<Author> authors = new ArrayList<Author>();
ArrayList<PubDate> pubs = new ArrayList<PubDate>();
books.add(new Book("Book Title", "Author Name", "Publishing Date"));
}
}
Context - So there are two classes that uses inheritance. The EmployeeService is the parent class and the EmployeeInfo is the child class.
What do I need help with - So I am trying to insert an arrayList to the parent class that combines the information of the experience and position and makes a new arrayList called serviceList.
And when I call a super() in the child class, I should be able to call the arrayList rather than the String variables (experience, position).
To put it short, I should basically be able to pass an arrayList as the third parameter in the child class employeeInfo method instead of String experience or String position
Parent class -
public class EmployeeService () {
private String experience;
private String position;
public EmployeeService (String experience, String position) {
this.setExperience (experience);
this.setPosition(position);
}
public String getExperience() {
return experience;
}
public void setExperience(String experience) {
this.experience = experience;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String toString() {
return "Experience - " + experience + "Position" + " - " + position;
}
}
Child class -
public class EmployeeInfo () {
private String firstName;
private String address;
public EmployeeInfo (String firstName, String address,String experience, String position) {
super(experience, position);
this.setFirstName (firstName);
this.setAddress(address);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "Name - " + firstName + "Address" + " - " + address + super.toString();
}
}
Just add the property and an additional constructor to the parent class as follows:
import java.util.ArrayList;
import java.util.List;
public class EmployeeService {
private String experience;
private String position;
private List<String> serviceList;
public EmployeeService (String experience, String position) {
this.setExperience (experience);
this.setPosition(position);
}
public EmployeeService (String experience, String position, List<String> serviceList) {
this(experience, position);
this.serviceList = new ArrayList<>(serviceList);
}
public String getExperience() {
return experience;
}
public void setExperience(String experience) {
this.experience = experience;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String toString() {
return "Experience - " + experience + "Position" + " - " + position;
}
}
Then adapt your child class:
import java.util.List;
public class EmployeeInfo extends EmployeeService {
private String firstName;
private String address;
public EmployeeInfo (String firstName, String address, String experience, String position) {
super(experience, position);
this.setFirstName (firstName);
this.setAddress(address);
}
public EmployeeInfo (String firstName, String address, String experience, String position, List<String> serviceList) {
super(experience, position, serviceList);
this.setFirstName (firstName);
this.setAddress(address);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "Name - " + firstName + "Address" + " - " + address + super.toString();
}
}
I'm trying to parse strig xml using JaxB API.
Below is my code
XML
http://www.devx.com/supportitems/showSupportItem.php?co=38898&supportitem=listing1
Pojo Classes
#XmlRootElement(name = "employees")
public class FileWrapper {
private LinkedHashSet<Employee> employees;
public LinkedHashSet<Employee> getEmployees() {
return employees;
}
#XmlElement(name = "employee")
public void setEmployees(LinkedHashSet<Employee> employees) {
this.employees = employees;
}
}
Employee.java
import javax.xml.bind.annotation.XmlAttribute;
public class Employee {
private String division;
private String firstname;
private String id;
private String title;
private String building;
private String room;
private String supervisor;
private String lastname;
public String getDivision() {
return division;
}
#XmlAttribute
public void setDivision(String division) {
this.division = division;
}
public String getFirstname() {
return firstname;
}
#XmlAttribute
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getId() {
return id;
}
#XmlAttribute
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
#XmlAttribute
public void setTitle(String title) {
this.title = title;
}
public String getBuilding() {
return building;
}
#XmlAttribute
public void setBuilding(String building) {
this.building = building;
}
public String getRoom() {
return room;
}
#XmlAttribute
public void setRoom(String room) {
this.room = room;
}
public String getSupervisor() {
return supervisor;
}
#XmlAttribute
public void setSupervisor(String supervisor) {
this.supervisor = supervisor;
}
public String getLastname() {
return lastname;
}
#XmlAttribute
public void setLastname(String lastname) {
this.lastname = lastname;
}
#Override
public String toString() {
return "ClassPojo [division = " + division + ", firstname = " + firstname + ", id = " + id + ", title = "
+ title + ", building = " + building + ", room = " + room + ", supervisor = " + supervisor
+ ", lastname = " + lastname + "]";
}
}
MainEntry
public class TestEntryPoint {
public static void main(String[] args) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(FileWrapper.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader("XML from above link as String");
FileWrapper person = (FileWrapper) unmarshaller.unmarshal(reader);
List<Employee> emp = new ArrayList<Employee>(person.getEmployees());
System.out.println(emp.get(0).getFirstname());
}
}
So when am trying to extract any tag's value its showing null. Is there any problem with my XML's data structure or pojo classes ? I'm stuck in this from last couple of hours.
What am doing wrong ? Canyone suggest please ?
Thanks
Annotations in Employee class refer to XML attributes instead of the elements.
Here is corrected version:
public class Employee {
private String division;
private String firstname;
private String id;
private String title;
private String building;
private String room;
private String supervisor;
private String lastname;
public String getDivision() {
return division;
}
#XmlElement(name = "division")
public void setDivision(String division) {
this.division = division;
}
public String getFirstname() {
return firstname;
}
#XmlElement(name = "firstname")
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getId() {
return id;
}
#XmlAttribute
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
#XmlElement(name = "title")
public void setTitle(String title) {
this.title = title;
}
public String getBuilding() {
return building;
}
#XmlElement(name = "building")
public void setBuilding(String building) {
this.building = building;
}
public String getRoom() {
return room;
}
#XmlElement(name = "room")
public void setRoom(String room) {
this.room = room;
}
public String getSupervisor() {
return supervisor;
}
#XmlElement(name = "supervisor")
public void setSupervisor(String supervisor) {
this.supervisor = supervisor;
}
public String getLastname() {
return lastname;
}
#XmlElement(name = "lastname")
public void setLastname(String lastname) {
this.lastname = lastname;
}
#Override
public String toString() {
return "ClassPojo [division = " + division + ", firstname = " + firstname + ", id = " + id + ", title = "
+ title + ", building = " + building + ", room = " + room + ", supervisor = " + supervisor
+ ", lastname = " + lastname + "]";
}
}
I have my entity class called employee and I want to soft delete my entity when I select and press delete button. I can able to select multiple employees as well, So in Java I used List of employee Entities and I want to update whole list into database table if I use merge of entityManager I can able to update only one row i.e only one entity so how do I solve this problem?
Here is some sample code.
#Entity
#Table(name="EmpInfo",schema="Auth")
public class EmpInfo{
#Id
#Column(name="EmpId")
private String userId;
#Column(name="EmailId")
private String emailId;
#Column(name="FirstName")
private String firstName;
#Column(name="LastName")
private String lastName;
#Column(name="MiddleName")
private String middleName;
#Column(name="UserAttributes")
private String userAttributes;
#Column(name="AddedDate")
private Timestamp addedDate;
#Column(name="ModifiedDate")
private Timestamp modifiedDate;
#Column(name="LastLoginDate")
private Timestamp lastLoginDate;
#Column(name="IsDeleted")
private int isDeleted;
#Column(name="AddedBy")
private String addedBy;
#Transient
private String addedByEmailId;
public String getAddedByEmailId() {
return addedByEmailId;
}
public void setAddedByEmailId(String addedByEmailId) {
this.addedByEmailId = addedByEmailId;
}
public EmpInfo() {
// TODO Auto-generated constructor stub
}
public EmpInfo(EmpInfo uInfo){
super();
this.userId=uInfo.userId;
this.emailId=uInfo.emailId;
this.firstName=uInfo.firstName;
this.lastName=uInfo.lastName;
this.middleName=uInfo.middleName;
this.userAttributes=uInfo.userAttributes;
this.addedDate=uInfo.addedDate;
this.lastLoginDate=uInfo.lastLoginDate;
this.modifiedDate=uInfo.modifiedDate;
this.addedBy=uInfo.addedBy;
this.roles=uInfo.roles;
}
public List<RoleName> getRoles() {
return roles;
}
public void setRoles(List<RoleName> roles) {
this.roles = roles;
}
public int getIsDeleted() {
return isDeleted;
}
public void setIsDeleted(int isDeleted) {
this.isDeleted = isDeleted;
}
public Date getAddedDate() {
return addedDate;
}
public void setAddedDate(Timestamp addedDate) {
this.addedDate = addedDate;
}
public Date getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(Timestamp modifiedDate) {
this.modifiedDate = modifiedDate;
}
public Date getLastLoginDate() {
return lastLoginDate;
}
public void setLastLoginDate(Timestamp lastLoginDate) {
this.lastLoginDate = lastLoginDate;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getUserAttributes() {
return userAttributes;
}
public void setUserAttributes(String userAttributes) {
this.userAttributes = userAttributes;
}
public String getAddedBy() {
return addedBy;
}
public void setAddedBy(String addedBy) {
this.addedBy = addedBy;
}
#Override
public String toString() {
return "EmpInfo [userId=" + userId + ", emailId=" + emailId + ", firstName=" + firstName + ", lastName="
+ lastName + ", middleName=" + middleName + ", userAttributes=" + userAttributes + ", addedDate="
+ addedDate + ", modifiedDate=" + modifiedDate + ", lastLoginDate=" + lastLoginDate + ", isDeleted="
+ isDeleted + ", addedBy=" + addedBy + "]";
}
}
public interface EmpInfoRepository extends JpaRepository<EmpInfo, String> {
}
use this to save list of entities as follows
#Autowired
private EmpInfoRepository empInfoRepository;
empInfoRepository.save(listOfEntity)
Since there is no custom implementation defined, implementation done by SimpleJpaRepository will be used, which will update the entities according to the #Id annotated field
I am passing an arrayList to a method to sort its data.
The data in the arrayList would contain multiple id numbers correlating to different students.
the array will also include other information. How do i sort the arrayList by its id #. I believe it will have to do with my created class which is included below
more code can be provided as necessary
public static class Student {
public String firstName;
public String lastName;
public Integer uid;
public StudentType type;
public Student(Student orig) {
this.firstName = orig.firstName;
this.lastName = orig.lastName;
this.uid = orig.uid;
this.type = orig.type;
}
// construct a new student with given fields
public Student(String firstName, String lastName, Integer newUid, StudentType type) {
this.firstName = firstName;
this.lastName = lastName;
this.uid = newUid;
this.type = type;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
//set type
public void setType(StudentType type) {
this.type = type;
}
//return type
public StudentType getType() {
return type;
}
public void setUid(Integer uid){
this.uid = uid;
}
public Integer getUid() {
return uid;
}
// return a string representation of the invoking object
public String toString() {
return firstName + " " + lastName + " " + uid + " " + type;
}
public static class Graduate extends Student {
public boolean thesis;
public ClassStanding study;
public String profName;
public Graduate(Student orig, boolean isThesis, ClassStanding study, String profName) {
super(orig);
thesis = isThesis;
this.study = study;
this.profName = profName;
}
public boolean getThesis() {
return thesis;
}
public void setThesis(Boolean thesis) {
this.thesis = thesis;
}
public ClassStanding getStudy() {
return study;
}
public void setStudy(ClassStanding study) {
this.study = study;
}
public String getProfName() {
return profName;
}
public void setProfName(String profName) {
this.profName = profName;
}
public String toString() {
return super.toString() + thesis + " " + study + " " + profName;
}
}
public static class UnderGraduate extends Student {
public Major major;
public Double overallGpa;
public Double majorGpa;
public ClassStanding study;
public UnderGraduate(Student orig, Major major, Double overallGpa, Double majorGpa, ClassStanding study) {
super(orig);
this.study = study;
this.major = major;
this.overallGpa = overallGpa;
this.majorGpa = majorGpa;
}
public void setMajor(Major major) {
this.major = major;
}
//return type
public Major getmMajor() {
return major;
}
public void setOverallGPA(Double overallGpa) {
this.overallGpa = overallGpa;
}
public Double getOverallGPA() {
return overallGpa;
}
public void setMajorGPA(Double majorGpa) {
this.majorGpa = majorGpa;
}
public Double getMajorGPA() {
return majorGpa;
}
public ClassStanding getStudy() {
return study;
}
public void setStudy(ClassStanding study) {
this.study = study;
}
public String toString() {
return study + " " + major + " " + overallGpa + " " + majorGpa;
}
}
}
Collections.sort(myListofStudents, new Comparator<Student>() {
#Override
public int compareTo(Student s1, Student s2) {
return s1.getUid().compareTo(s2.getUid());
}
});
Alternatively, you can have Student implement Comparable<Student>, which means including a .compareTo().
NB: If you're going to override .compareTo(), you should override .equals(), which means you should override hashCode().
Define a Comparator<Student> and use it with Collections.sort:
public class StudentComparator implements Comparator<Student> {
#Override
public int compare (Student s1, Student s2) {
// Your comparison logic here
}
}
...then...
Collections.sort(data, new StudentComparator());
See the JavaDoc on Comparator for more information.
Create a Comparator for Student, then use Collections.sort()