JAXB project help needed - java

I need help with this issue in JAXB, while I run the project I always got the error below
Property assignment is present but not specified in #XmlType.propOrder
this problem is related to the following location:
at public int edu.asu.cse446.sample1.server.test.GradBook.getAssignment()
at edu.asu.cse446.sample1.server.test.GradBook
This is my code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.asu.cse446.sample1.server.test;
/**
*
* #author luayalmamury
*/
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author luayalmamury
*/
#XmlRootElement
#XmlType(propOrder={
"id",
"Assignment",
"MidTerm",
"Quiz",
"ClassLab"})
public class GradBook {
private static final Logger LOG = LoggerFactory.getLogger(GradBook.class);
private int id;
private int Assignment;
private int MidTerm;
private int Quiz;
private int ClassLab;
public GradBook(){
LOG.info(" Creating Student Grade Object");
}
public int getId() {
return id;
}
#XmlAttribute // Read it in XML Format
public void setId(int id) {
LOG.info(" Setting Student Id", id);
this.id=id;
LOG.debug(" Update Student Id",this);
}
public int getAssignment() {
return Assignment;
}
#XmlElement
public void setAssignment(int Assignment) {
LOG.info(" Create Student Assignment",Assignment);
this.Assignment=Assignment;
LOG.debug("Update Student Assignment",this);
}
public int getMidTerm() {
return MidTerm;
}
#XmlElement // Read it in XML Format
public void setMidTerm(int MidTerm) {
LOG.info(" Create Student MidTerm ",MidTerm);
this.MidTerm=MidTerm;
LOG.debug("Update Student MidTerm ",this);
}
public int getQuiz() {
return Quiz;
}
#XmlElement // Read it in XML Format
public void setQuiz(int Quiz) {
LOG.info(" Create Student Quiz",Quiz);
this.Quiz=Quiz;
LOG.debug("Update Student Assignment",this);
}
public int getClassLab() {
return ClassLab;
}
#XmlElement // Read it in XML Format
public void setClassLab(int ClassLab) {
LOG.info(" Create Class Lab",ClassLab);
this.ClassLab=ClassLab;
LOG.debug("Update Student Class Lab",this);
}
#Override
public String toString() {
return "GradeBook{" + "id=" + id + ", Assignment=" + Assignment + ",MidTerm=" + MidTerm + " Quiz=" + Quiz + ", ClassLab=" + ClassLab + '}';
}
}

Okay. This is why they say - follow standard and conventions.
Problem is - you have member variable names capitalized.
Change them to something like - "id", "assignment", "midTerm", "quiz", "classLab" and it should work.

Related

Hibernate looking for column in wrong table

I have this query that works in native SQL and Hibernate in Java.
It breaks with error that says
could not resolve property: entityName …
(or amount) when I use pageable to sort results for one of those two columns.
Also Hibernate fails to map items to Page<McConsumerBalanceEntity> and returns pageimpl of objects so I am using List<Object[]> to map items manually which works.
Hibernate version is 5.3.7. Database is PostgreSQL 10.5. Spring Boot is 2.1.0
#Query(
value = "select csr.id, csr.firstName, csr.lastName, " +
"sum (case " +
"when acs.type = 'PAYMENT' then acs.amount " +
"else -acs.amount " +
"end) as amount, " +
"me.id as entityId, " +
"me.name as entityName " +
"from McConsumerEntity csr, EbAccountConsumerEntity acs, McEntity me " +
"where csr.idMcEntity in :childEntityIds " +
"and csr.idMcEntity = me.id " +
"and csr.id = acs.idMcConsumer " +
"and amount > 0 " +
"group by csr.id, me.id, me.name ")
public class McConsumerBalanceEntityGenerated implements Serializable {
private static final long serialVersionUID = 217L;
#Id
#GeneratedValue(generator = "sequence_null", strategy = GenerationType.SEQUENCE)
#SequenceGenerator(name = "sequence_null",
sequenceName = "sequence_null", allocationSize = 100)
protected Long id;
#Column(name = "amount")
protected BigDecimal amount;
#Column(name = "entity_id")
protected Long entityId;
#Column(name = "entity_name")
protected String entityName;
#Column(name = "first_name")
protected String firstName;
#Column(name = "last_name")
protected String lastName;
//---------------------------------------------------------------------
public McConsumerBalanceEntityGenerated() {
super();
}
/*
//---------------------------------------------------------------------
private McConsumerBalanceEntity(Builder builder) {
this.id = builder.id;
}
*/
//---------------------------------------------------------------------
public Long getId() {
return id;
}
/**
* Access method for the amount property.
*
* #return the current value of the amount property
*/
public BigDecimal getAmount() {
return this.amount;
}
/**
* Sets the value of the amount property.
*
* #param aAmount the new value of the version property
*/
public void setAmount(BigDecimal aAmount) {
this.amount = aAmount;
}
/**
* Access method for the entityId property.
*
* #return the current value of the entityId property
*/
public Long getEntityId() {
return this.entityId;
}
/**
* Sets the value of the entityId property.
*
* #param aEntityId the new value of the version property
*/
public void setEntityId(Long aEntityId) {
this.entityId = aEntityId;
}
/**
* Access method for the entityName property.
*
* #return the current value of the entityName property
*/
public String getEntityName() {
return this.entityName;
}
/**
* Sets the value of the entityName property.
*
* #param aEntityName the new value of the version property
*/
public void setEntityName(String aEntityName) {
this.entityName = aEntityName;
}
/**
* Access method for the firstName property.
*
* #return the current value of the firstName property
*/
public String getFirstName() {
return this.firstName;
}
/**
* Sets the value of the firstName property.
*
* #param aFirstName the new value of the version property
*/
public void setFirstName(String aFirstName) {
this.firstName = aFirstName;
}
/**
* Access method for the lastName property.
*
* #return the current value of the lastName property
*/
public String getLastName() {
return this.lastName;
}
/**
* Sets the value of the lastName property.
*
* #param aLastName the new value of the version property
*/
public void setLastName(String aLastName) {
this.lastName = aLastName;
}
/* //---------------------------------------------------------------------
public static class Builder {
private Long id;
public Builder setId(Long id) {
this.id = id;
return this;
}
public McConsumerBalanceEntity build() {
return new McConsumerBalanceEntity(this);
}
}*/
}
As the error clearly states that could not resolve property: entityName, which means the column does not exists in the Entity which it is trying to fetch. And I can observe that in your SQL native query, You are trying to fetch it from me.Name and as per the entity you have provided it is present in McConsumerBalanceEntityGenerated. So try joining this Entity and fetch the entityName from this table/Entity.
You are not saying which Hibernate version your are using or what database, so everyone here has to guess about these questions and many more. If you ever want to receive good answers, consider giving more details in your questions or pay a consultant to do the work for you. The way you are asking questions here is as if you were asking a mechanic "My Ford makes a strange noise when driving fast, what's the problem?".
Having said that, I'll start guessing by assuming you are using Oracle. Maybe you are running into an issue similar to the following one: https://discourse.hibernate.org/t/issues-in-migrating-from-hibernate-5-3-3-to-5-3-4/5679/2
Also Hibernate fails to map items to Page and returns pageimpl of objects so I am using List<Object[]> to map items manually which works.
That is expected. Hibernate does not do "inference" or anything like that. If you want McConsumerBalanceEntity to be returned, then use a JPQL constructor expression.

Not able to parse fields with underscores

I have a column name viewed_by on Firebase server
Test
|
|--viewed_by: 30
On the app I have a POJO class which has the member viewed_by
Test.class has member
private int viewed_by;
In onDataChange function when I receive the data, I get the Test object using the getValue function
Test t = dataSnapshot.getValue(Test.class);
But I get the value as 0 instead of 30.
If I change the field name from viewed_by to viewedBy (both on server and POJO class), I get the expected value (30)
Is it a parsing issue in getValue function? Or the field name are not supposed to have underscores in the name?
Jus figured it out, had to change the function names as well from ViewedBy to Viewed_By for it to work with viewed_by field
/**
*
* #return
* The viewed_by
*/
public int getViewed_By() {
return viewed_by;
}
/**
*
* #param viewed_by
* The viewed_by
*/
public void setViewed_By(int viewed_by) {
this.viewed_by = viewed_by;
}
Another option is to just declare the properties like below-using #PropertyName("property_name") in your model and use your getter and setter just like you like to do.
public class Actor {
#NonNull
#PrimaryKey
#ColumnInfo(name = "profile_id")
#PropertyName("profile_id")
private String profileId;
#ColumnInfo(name = "name")
#PropertyName("name")
private String name;
#PropertyName("profile_id")
public String getProfileId() {
return profileId;
}
#PropertyName("profile_id")
public void setProfileId(String profileId) {
this.profileId = profileId;
}
#PropertyName("name")
public String getName() {
return name;
}
}

JAXB - Cannot figure out how to use refID properly

Goal: Marshalling and unmarshalling the clinic.xml correctly
Problem: Reading out the id's of the physiotherapist (people who work in the clinic)
This is the clinic.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<clinic clinicNumber="1">
<name>ClinicStackOverFlow</name>
<address>Deadbrains</address>
<zipCode>SomeZip</zipCode>
<city>City</city>
<phoneNumber>069441341341</phoneNumber>
<!-- LIST OF THE ID's of physiotherapists that work here -->
<physiotherapists>1</physiotherapists>
<physiotherapists>2</physiotherapists>
</clinic>
Clinic.java
package fysio.shared.domain;
import com.sun.deploy.xml.XMLable;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.List;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Clinic {
/**
* The identifier of a clinic
*/
#XmlID
#XmlAttribute
#XmlJavaTypeAdapter(IDStringAdapter.class)
private String clinicNumber;
/**
* The name of a clinic
*/
private String name;
/**
* The address where the clinic is located
*/
private String address;
/**
* The zip code of a clinic
*/
private String zipCode;
/**
* The city a clinic is located in
*/
private String city;
/**
* The phone number of a clinic
*/
private String phoneNumber;
#XmlIDREF
private List<Physiotherapist> physiotherapists;
/**
* The default constructor for Jaxb
*/
public Clinic() {
}
public Clinic(String clinicNumber, String name, String address, String zipCode, String city, String phoneNumber, List<Physiotherapist> physiotherapists) {
this.clinicNumber = clinicNumber;
this.name = name;
this.address = address;
this.zipCode = zipCode;
this.city = city;
this.phoneNumber = phoneNumber;
this.physiotherapists = physiotherapists;
}
/**
* Returns the number of a clinic
*
* #return The number of a clinic
*/
public String getClinicNumber() {
return clinicNumber;
}
/**
* Sets the number of a clinic
*
* #param clinicNumber the number of a clinic
*/
public void setClinicNumber(String clinicNumber) {
this.clinicNumber = clinicNumber;
}
public List<Physiotherapist> getPhysiotherapists() {
return physiotherapists;
}
/**
* Sets the physiotherapists of a clinic
*
* #param physiotherapists The Physiotherapists of a clinic
*/
public void setPhysiotherapists(List<Physiotherapist> physiotherapists) {
this.physiotherapists = physiotherapists;
}
/**
* adds a physiotherapist to a clinic
*
* #param physiotherapist The physiotherapist that needs to be added to a clinic
*/
public void addPhysiotherapist(Physiotherapist physiotherapist) {
physiotherapists.add(physiotherapist);
}
}
We have a list of physiotherapists (in xml)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<physiotherapists>
<physiotherapist physiotherapistNumber="1">
<clinic>1</clinic>
<name>Henk</name>
</physiotherapist>
<physiotherapist physiotherapistNumber="2">
<clinic>8</clinic>
<name>Klaas</name>
</physiotherapist>
</physiotherapists>
Physiotherapist.java (singular)
package fysio.shared.domain;
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
public class Physiotherapist {
#XmlAttribute
#XmlID
private String physiotherapistNumber;
#XmlIDREF
private Clinic clinic;
private String name;
public Physiotherapist() {
//Default empty constructor for JAXB
}
public Physiotherapist(String name, Clinic clinic) {
this.clinic = clinic;
this.name = name;
}
public Clinic getClinic() {
return clinic;
}
public String getPhysiotherapistNumber() {
return physiotherapistNumber;
}
public void setPhysiotherapistNumber(String physiotherapistNumber) {
this.physiotherapistNumber = physiotherapistNumber;
{}
}
Physiotherapists.java (plural)
#XmlRootElement(name = "physiotherapists")
#XmlAccessorType(XmlAccessType.FIELD)
public class Physiotherapists {
#XmlElement(name = "physiotherapist")
private List<Physiotherapist> physiotherapistList;
public Physiotherapists() {
//empty constructor for xml parsing
physiotherapistList = new ArrayList<Physiotherapist>();
}
public List<Physiotherapist> getPhysiotherapistList() {
return physiotherapistList;
}
}
And finally the unmarshalling part:
try {
JAXBContext jc = JAXBContext.newInstance(Clinic.class, Physiotherapist.class, Physiotherapists.class);
File clinicXML = new File("src/test/resources/data/xml/clinic.data");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Clinic clinicXMLData = (Clinic) unmarshaller.unmarshal(clinicXML);
File fysiotherapistXML = new File("src/test/resources/data/xml/physiotherapist.data");
Unmarshaller unmarshaller2 = jc.createUnmarshaller();
Physiotherapists ph = (Physiotherapists) unmarshaller2.unmarshal(fysiotherapistXML);
} catch (JAXBException e) {
e.printStackTrace();
}
Both unmarshallers do the best they can. I get a nice list of physiotherapist from unmarshaller 2 but I don't get anything about the physiotherapists from the clinic unmarshaller:
http://imgur.com/Mpcgm8t (stack didn't let me upload pics)
I kinda lost it... not knowing anymore whats wrong and correct. Tried many solutions online, understand the most of them but still missing something.
(It's a school project and it's not yet refactored)
How should it be possible to get the physiotherapists (PT) references into the Clinic objects when unmarshalling the PT list has no connection whatsoever to those Clinic objects? The Clinics have been built from the XML data, and there are no PTs in it, period.
For XmlID and XmlIDREF to work, i.e., to store an object reference in the field annotated XmlIDREF there must be an object of suitable type and with the matching value in its XmlID field within the same XML file.
You must combine the XML data into a single file.
Seeing that you reference the Clinic from the PT and the PTs from the Clinic, I'm afraid that you'll encounter difficulties in one direction even then. (I might be wrong - it's too long since I tried this.)
Now I think that you may not want to merge the XML files anyway. To solve your predicament, I suggest that you drop the ID and IDREF annotations and set the links "by hand". A single pass through the PT list is sufficient, a simple and robust solution.

java OOP link between classes (first time programming in java)

I would like to link two classes where the consultant have an ID and I would like to send this ID to the customer so a customer will be assigned to the consultant.
I created a class for the consultant with the ID, name and surname and the same for the customer. I am trying to get the ID from the consultant using the code below
Consultant newconsultant = new Consultant(Consultant.getConsultantID());
The consultantID is the id of the consultant in the class consultant.I am stuck and I appreciate any help with any information for this issue.
Consultant code:
public class Consultant extends Person implements Serializable {
public String ConsultantID;
private String Consfirstname;
private String Conslastname;
Consultant(String consultantID) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
/**
* #return the ConsultantID
*/
public String getConsultantID() {
return ConsultantID;
}
/**
* #param ConsultantID the ConsultantID to set
*/
public void setConsultantID(String ConsultantID) {
this.ConsultantID = ConsultantID;
}
/**
* #return the Consfirstname
*/
public String getConsfirstname() {
return Consfirstname;
}
/**
* #param Consfirstname the Consfirstname to set
*/
public void setConsfirstname(String Consfirstname) {
this.Consfirstname = Consfirstname;
}
/**
* #return the Conslastname
*/
public String getConslastname() {
return Conslastname;
}
/**
* #param Conslastname the Conslastname to set
*/
public void setConslastname(String Conslastname) {
this.Conslastname = Conslastname;
}
Customers Code:
public class Customer extends Person implements Serializable {
private String CustomerID;
public String Custfirstname;
public String Custlastname;
private Consultant Consultant;
public String CID;
Consultant newconsultant = new Consultant(Consultant.getConsultantID());
/**
* #return the CustomerID
*/
public String getCustomerID() {
return CustomerID;
}
/**
* #param CustomerID the CustomerID to set
*/
public void setCustomerID(String CustomerID) {
this.CustomerID = CustomerID;
}
/**
* #return the Custfirstname
*/
public String getCustfirstname() {
return Custfirstname;
}
/**
* #param Custfirstname the Custfirstname to set
*/
public void setCustfirstname(String Custfirstname) {
this.Custfirstname = Custfirstname;
}
/**
* #return the Custlastname
*/
public String getCustlastname() {
return Custlastname;
}
/**
* #param Custlastname the Custlastname to set
*/
public void setCustlastname(String Custlastname) {
this.Custlastname = Custlastname;
}
/**
* #return the Consultant
*/
public Consultant getConsultant() {
return Consultant;
}
/**
* #param Consultant the Consultant to set
*/
public void setConsultant(Consultant Consultant) {
this.Consultant = Consultant;
}
/**
* #return the CID
*/
public String getCID() {
return CID;
}
/**
* #param CID the CID to set
*/
public void setCID(String CID) {
this.CID = CID;
}
There are two forms were the consultant and customers details will be inputted.
In your Customer class are using Consultant class incorrectly.
You are trying to access a non-static method in a static way.
It is not clear if you have getter and setter methods then why you are trying to create a Consultant instance saperately. This looks to be incrrect in design perspective
What you can do is:
From a another class say MyApplication you can first set Consultant and using the same object you can pass it to Customer.
Consultant consultant = new Consultant("123");
consultant.setConsultantID("123");
consultant.setConsfirstname("firstname");
consultant.setConslastname("lastname");
Customer customer = new Customer();
customer.setConsultant(consultant);
This way your customer instance will have a reference to a Consultant instance in it with all the details. This is has-a relationship also called as a composition.
You are calling getConsultatntId in a static way. If your variable is not static you should call the method from a instance of the object (instead of the Class name)
That would look like:
Consumer c = new Consumer(1000);
Consultant con = new Consultant(c.getConsumerId());
Note that I'm calling getId from 'c' that is a instance of Consumer.
Firstly, you should make your Consultant constructor public. Secondly, you cannot access a non-static method in a static way, meaning you need to make a public static String getConsultantID() or something like this:
Consultant c = new Consultant("c2418");
Consultant d = new Consultant(c.getConsultantID());
Two things:
1:
To give a customer a consultant id, you would use an instance method instead of a static method:
Consultant james = new Consultant("123");
Customer customer1 = new Customer(james.getConsultantId());
2:
Consider using an Array or List of Customers in the Consultant class. It would seem more appropriate for a consultant to have multiple customers, rather than a single Consultant to Client relationship. In this way, you create a one-to-many relationship between Consultants and Customers.
Customers would have a reference to their Consultant, and Consultants would have a reference to all of their Customers.
public class Consultant extends Person implements Serializable {
public String ConsultantID;
private String Consfirstname;
private String Conslastname;
private ArrayList<Customer> customers;
Consultant(String consultantID) {
customers = new ArrayList();
}
public boolean addCustomer(Customer c){
customers.add(c);
}
//snipped for brevity
}
public class Customer extends Person implements Serializable {
private String CustomerID;
public String Custfirstname;
public String Custlastname;
public String CID;
Customer(Consultant c){
CID = c.getConsultantID();
}
//snipped for brevity
}
When you create the relationship, you'd create the customer first, assign them a consultant, then add the customer to the Consultant.
Consultant kingston = new Consultant("kingston88"); //created elsewhere
Customer jack = new Customer(kingston); //create customer, give id
kingston.add(jack); //assign customer to consultant

NullPointerException #one-to-one mapping

Hi all I've a small problem to map one-to-one using JPA 2 persistence with EclipseLink vendor and maybe any of you will be able to help.
I want to map one table with another but the field in second table is optional. So I've created #Entity for the first table:
Amp class
private AmpApInfo ampApInfo;
private String apid;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public String getApid() {
return this.apid;
}
public void setApid(String apid) {
this.apid = apid;
}
#OneToOne
#JoinColumn(name="apid")
public AmpApInfo getAmpApInfo() {
return this.ampApInfo;
}
public void setAmpApInfo(AmpApInfo ampApInfo) {
this.ampApInfo = ampApInfo;
}
and #Entity for the second table
AmpApInfo class
private String apid;
private Amp amp;
private String prodOrderNo;
public AmpApInfo() {
}
#OneToOne(optional=true, mappedBy="ampApInfo")
public Amp getAmp() {
return this.amp;
}
public void setAmp(Amp amp) {
this.amp = amp;
}
#Id
public String getApid() {
return apid;
}
public void setApid(String apid) {
this.apid = apid;
}
#Column(name="prod_order_no")
public String getProdOrderNo() {
return this.prodOrderNo;
}
public void setProdOrderNo(String prodOrderNo) {
this.prodOrderNo = prodOrderNo;
}
Now when I want to find prodOrderNo like
public Amp selectProdInfo(String name) {
// TODO Auto-generated method stub
Amp amp = Transactions.getEntityManager().find(Amp.class, name.trim());
System.out.println("order number" + amp.getAmpApInfo().getProdOrderNo());
return amp;
}
I'm expecting to get null cos its not there but I'm getting java.lang.NullPointerException on the line
System.out.println("order number" + amp.getAmpApInfo().getProdOrderNo());
Any one can help???
Either amp is null or amp.getAmpApInfo() is returning null, and you are trying to call methods on them.

Categories

Resources