Here I got two classes "Student" and "Course":
#XmlRootElement
class Student {
private String name;
private HashMap<String, Course> courses;
public Student() {}
public Student(String name, HashMap<String, Course> courses) {
super();
this.name = name;
this.courses = courses;
}
// -------------------------getters
#XmlAttribute(name="name")
public String getName() {
return name;
}
#XmlElement(name = "course")
public HashMap<String, Course> getCourses() {
return courses;
}
//---------------------------setters
public void setName(String name) {
this.name = name;
}
public void setCourses(HashMap<String, Course> courses) {
this.courses = courses;
}
}
#XmlRootElement
class Course {
private String id;
private String name;
public Course() {}
public Course(String id, String name) {
super();
this.id = id;
this.name = name;
}
#XmlID
#XmlAttribute(name = "id")
public String getId() {
return id;
}
#XmlAttribute(name = "name")
public String getName() {
return name;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
I want to serialize the Student class using JaxB annotations to something like the following:
<Student name="David">
<courses>
<Course id="1" name="Fundamentals of Programming"/>
<Course id="2" name="Advanced Programming">
</courses>
</Student>
Note that I want courses field of the Student to be HashMap and when unmarshalling the serialized Object use id as the key for each Course.
You can use an XmlAdapter for this use case. An XmlAdapter allows you to programmatically convert an object to another type for the purpose of marshalling/unmarshalling. For your use case you will convert the HashMap to an instance of an object that has a collection of Course.
http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html
Related
Hi I am trying to convert an xml file into Java Objects using JAXB and I am very new to java. I have created the pojo classes and added some annotations but I am not sure whether they are right? I have spent hours in google but couldn't find what is wrong.
This is my xml :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<question id="1">
<answers>
<answername>java is a programming language</answername>
<id>101</id>
<postedby>ravi</postedby>
</answers>
<answers>
<answername>java is a platform</answername>
<id>102</id>
<postedby>john</postedby>
</answers>
<questionname>What is java?</questionname>
<marks set=50>
<longAnswer set=45/>
<shortAnswer set=30/>
</marks>
</question>
Pojo classes:
#XmlRootElement(name="question")
public class Question {
private int id;
private String questionname;
private List<Answer> answers;
private List<Marks> marks;
public Question() {}
public Question(int id, String questionname, List<Answer> answers, List<Marks> marks) {
super();
this.id = id;
this.questionname = questionname;
this.answers = answers;
this.marks = marks;
}
#XmlElement(name="marks")
public List<Marks> getMarks() {
return marks;
}
public void setMarks(List<Marks> marks) {
this.marks = marks;
}
#XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#XmlElement
public String getQuestionname() {
return questionname;
}
public void setQuestionname(String questionname) {
this.questionname = questionname;
}
#XmlElement
public List<Answer> getAnswers() {
return answers;
}
public void setAnswers(List<Answer> answers) {
this.answers = answers;
}
}
public class Answer {
private int id;
private String answername;
private String postedby;
public Answer() {}
public Answer(int id, String answername, String postedby) {
super();
this.id = id;
this.answername = answername;
this.postedby = postedby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAnswername() {
return answername;
}
public void setAnswername(String answername) {
this.answername = answername;
}
public String getPostedby() {
return postedby;
}
public void setPostedby(String postedby) {
this.postedby = postedby;
}
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class Marks {
private LongAnswer longAnswer ;
private ShortAnswer shortAnswer;
private String set;
#XmlAttribute
public String getSet() {
return set;
}
public void setSet(String set) {
this.set = set;
}
#XmlElement(name="longAnswer")
public LongAnswer getLongAnswer() {
return longAnswer;
}
public void setLongAnswer(LongAnswer longAnswer) {
this.longAnswer = longAnswer;
}
#XmlElement(name="shortAnswer")
public ShortAnswer getShortAnswer() {
return shortAnswer;
}
public void setShortAnswer(ShortAnswer shortAnswer) {
this.shortAnswer = shortAnswer;
}
}
public class LongAnswer {
private String set;
public String getSet() {
return set;
}
public void setSet(String set) {
this.set = set;
}
public class ShortAnswer {
private String set;
public String getSet() {
return set;
}
public void setSet(String set) {
this.set = set;
}
}
Can anyone tell me how to annotate the 'marks' model class and how to set 'longAnswer' and 'shortAnswer' field. Because i am getting null values for them.
You should annotate your set properties with #XmlAttribute. Otherwise it looks quite fine.
What you could also do is create an XML Schema for you XML and compile it.
I'm trying to use spring-data-cassandra (1.1.2.RELEASE) and I'm running into an issue with the lack of embeddable type mapping in JPA parlance.
I have an entity class like this:
#Table
public class Customer {
private UUID id;
private String name;
private Address address;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
and the embeddable Address class:
public class Address {
private String address;
private String city;
private String state;
private String zip;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
My cassandra table:
create table customer (
id uuid primary key,
name text,
address text,
city text,
state text,
zip text
);
I want the properties of Address to be mapped into the containing entity, I don't want a separate table for addresses. In JPA, I believe I'd use an #Embeddable annotation. Is there some similar construct in spring-data-cassandra?
Embeddable types are not yet supported by spring-data-cassandra. A feature request is available at DATACASS-167.
The only possible part of an entity to embed is the primary key. If your primary key consists of multiple fields, you can externalize that fields into a separate class and use it afterwards with the #PrimaryKey annotation.
Comment.java
#Table("comments")
public class Comment {
#PrimaryKey
private CommentKey pk;
private String text;
}
CommentKey.java
#PrimaryKeyClass
public class CommentKey implements Serializable {
private static final long serialVersionUID = -7871651389236401141L;
#PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private String author;
#PrimaryKeyColumn(ordinal = 1)
private String company;
}
HTH, Mark
I have the following inheritance hierarchy defined in java.
public class BaseModel extends HashMap<String, Object> {
public String getString(String key) {
return (String)this.getOrDefault(key, "EMPTY");
}
}
public class Entity extends BaseModel {
private String id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Now in a groovy script I try to do the following:
Entity entity = new Entity();
entity.id = "101";
entity.name = "Apple"
and "id" and "name" are not recognized. The funny thing is they are recognized if I do one of the following:
not inherit Entity from BaseModel
Rather than inherit BaseModel from HashMap, make HashMap a data member of BaseModel
inherit Entity directly from HashMap
At first I thought that groovy is not recognizing the "id" and "name" syntax because of extending HashMap, but #3 above proves that incorrect. I am stumped as to why this is not being recognized at this point. Can someone help me out? It should be easy enough to copy paste this and try it out yourself.
The problem seems to be the setters and getters inside the Entity Class, everything in groovy is public and it creates all the getters and setters methods.
I tested the next code in the groovy console and it worked.
public class BaseModel extends HashMap<String, Object> {
public String getString(String key) {
return (String)this.getOrDefault(key, "EMPTY");
}
}
public class Entity extends BaseModel {
private String id;
private String name;
}
Entity entity = new Entity();
entity.id = "101";
entity.name = "Apple"
println entity.id
It prints 101 in the groovyConsole output screen.
When Entity is extending from BaseModel or directly a HashMap, Entity becomes a Map. So, when we say entity.id, Groovy is trying to find an entry in the map whose key is 'id'. As there is no such entry, it prints out null.
public class Entity extends HashMap<String, String> {
private String id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Entity entity = new Entity();
entity.id = "101";
entity.name = "Apple"
println entity.id //prints null
But when Entity is not extending from BaseModel anymore, entity.id will be interpreted just as a member of Entity.
public class Entity {
private String id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Entity entity = new Entity();
entity.id = "101";
entity.name = "Apple"
println entity.id //prints 101
take a look at this code :
#XmlRootElement
class Course {
private int id;
private String name;
public Course() {}
public Course(int id, String name) {
super();
this.id = id;
this.name = name;
}
#XmlAttribute(name = "id")
public int getId() {
return id;
}
#XmlAttribute(name = "name")
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
class CourseAdapter extends XmlAdapter<Integer, Course>{
#Override
public Course unmarshal(Integer v) throws Exception {
// what to do hereeeeeeeeeeeeeeee????!!!!
// I want the Course with the id = v unmarshalled from
// the same xml I am unmarshalling at the moment
}
#Override
public Integer marshal(Course v) throws Exception {
return v.getId();
}
}
#XmlRootElement
class Offering {
private int id;
private Course course;
private int capacity;
public Offering() {}
public Offering(int id, Course course, int capacity) {
super();
this.id = id;
this.course = course;
this.capacity = capacity;
}
#XmlAttribute(name = "id")
public int getId() {
return id;
}
#XmlJavaTypeAdapter(CourseAdapter.class)
#XmlAttribute(name = "course")
public Course getCourse() {
return course;
}
#XmlAttribute(name = "capacity")
public int getCapacity() {
return capacity;
}
public void setId(int id) {
this.id = id;
}
public void setCourse(Course course) {
this.course = course;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
}
#XmlRootElement
class Department {
private String name;
private ArrayList<Course> courses;
private ArrayList<Offering> offerings;
public Department(){}
public Department(String name, ArrayList<Course> courses, ArrayList<Offering> offerings) {
super();
this.name = name;
this.courses = courses;
this.offerings = offerings;
}
#XmlAttribute(name = "name")
public String getName() {
return name;
}
#XmlElement
public ArrayList<Course> getCourses() {
return courses;
}
#XmlElement
public ArrayList<Offering> getOfferings() {
return offerings;
}
public void setName(String name) {
this.name = name;
}
public void setCourses(ArrayList<Course> courses) {
this.courses = courses;
}
public void setOfferings(ArrayList<Offering> offerings) {
this.offerings = offerings;
}
}
I have marshalled my Department and this is the result:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<department name="ece">
<courses id="1" name="farsi"/>
<courses id="2" name="dini"/>
<courses id="2" name="riazi"/>
<offerings capacity="10" course="1" id="1"/>
<offerings capacity="20" course="2" id="2"/>
</department>
the problem is I do not know how to unmarshal course with the id = v from this very xml which is being unmarshalled via "unmarshal" function of CourseAdapter .
You can use #XmlID/#XmlIDREF for this use case without requiring an XmlAdapter.
http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html
I am displaying a ProductList, which is made up of Product objects. The Product has attributes of type int, string, string, and int. The data is being pulled from the database and I build a ProductList. The data is in the database, I can see it, but when I display the table, the table shows 0's for the 2 int columns, and blanks in the String columns. Here is the ProductList:
#ManagedBean
public class ProductList {
private ArrayList<Product> allProducts;
public ProductList(){
allProducts = DatabaseConnector.getAllProducts();
}
public ArrayList<Product> getAllProducts(){
return allProducts;
}
public void setAllProducts(ArrayList<Product> allProducts){
this.allProducts = allProducts;
}
}
And here is the Product bean:
#ManagedBean
public class Product {
private int id;
private String productName;
private String description;
private int quantity;
public Product() {
}
public void setId(int id) {
this.id = id;
}
public void setProductName(String productName) {
this.productName = productName;
}
public void setDescription(String description) {
this.description = description;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getId() {
return id;
}
public String getProductName() {
return productName;
}
public String getDescription() {
return description;
}
public int getQuantity() {
return quantity;
}
}
Should I change the scope of the beans?
Add scope annotation to your bean , for example #RequestScoped or #ViewScoped
otherwise it will be the default scope which is #NoneScoped