Good evening all,
I am trying to update a field in my page and keep getting an exception thrown.
I have a main object "car" that has a foreign key to "Model"
when I built my page I created a select box to list all the models. So now when I choose a model and submit the form I get the exception thrown below. All of the fields that are in the Car table update fine as long as I do not include the model field. Once I try to include the model field, code breaks on submitting.
org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/sellcar.jsp at line 28
25: <c:forEach items="${cars}" var="car">
26: <option
27: value='<c:out value="${car.model.modId}"/>'
28: <c:if test="${car.model.model == status.value.model}">SELECTED</c:if>>
29: <c:out value="${car.model.model}" />
30: </option>
31: </c:forEach>
**Stacktrace:**
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:111)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1045)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:810)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:723)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:360)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.el.PropertyNotFoundException: Property 'model' not found on type java.lang.String
javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:214)
javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:191)
javax.el.BeanELResolver.property(BeanELResolver.java:300)
javax.el.BeanELResolver.getValue(BeanELResolver.java:81)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
org.apache.el.parser.AstValue.getValue(AstValue.java:123)
org.apache.el.parser.AstEqual.getValue(AstEqual.java:38)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:938)
org.apache.jsp.WEB_002dINF.jsp.sellcar_jsp._jspx_meth_c_005fif_005f0(sellcar_jsp.java:847)
org.apache.jsp.WEB_002dINF.jsp.sellcar_jsp._jspx_meth_c_005fforEach_005f0(sellcar_jsp.java:791)
org.apache.jsp.WEB_002dINF.jsp.sellcar_jsp._jspService(sellcar_jsp.java:107)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:111)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1045)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:810)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:723)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:360)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
</b>
My Models:
Car:
package com.usedcarsearch.domain;
// Generated Feb 19, 2013 10:31:37 PM by Hibernate Tools 3.4.0.CR1
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
/**
* Car generated by hbm2java
*/
public class Car implements java.io.Serializable {
private Integer carId;
private Make make;
private State state;
private Model model;
private City city;
private String vin;
private int year;
private String image;
private String engine;
private String trans;
private String mileage;
private BigDecimal price;
private String color;
private BigDecimal hwyMpg;
private BigDecimal cityMpg;
private String address;
private String accessories;
private String comments;
private Set buyers = new HashSet(0);
private Set ownerships = new HashSet(0);
private Set carSearchLists = new HashSet(0);
public Car() {
}
public Car(Make make, State state, Model model, City city, int year,
String engine, String trans, String mileage) {
this.make = make;
this.state = state;
this.model = model;
this.city = city;
this.year = year;
this.engine = engine;
this.trans = trans;
this.mileage = mileage;
}
public Car(Make make, State state, Model model, City city, String vin,
int year, String image, String engine, String trans,
String mileage, BigDecimal price, String color, BigDecimal hwyMpg,
BigDecimal cityMpg, String address, String accessories,
String comments, Set buyers, Set ownerships, Set carSearchLists) {
this.make = make;
this.state = state;
this.model = model;
this.city = city;
this.vin = vin;
this.year = year;
this.image = image;
this.engine = engine;
this.trans = trans;
this.mileage = mileage;
this.price = price;
this.color = color;
this.hwyMpg = hwyMpg;
this.cityMpg = cityMpg;
this.address = address;
this.accessories = accessories;
this.comments = comments;
this.buyers = buyers;
this.ownerships = ownerships;
this.carSearchLists = carSearchLists;
}
public Integer getCarId() {
return this.carId;
}
public void setCarId(Integer carId) {
this.carId = carId;
}
public Make getMake() {
return this.make;
}
public void setMake(Make make) {
this.make = make;
}
public State getState() {
return this.state;
}
public void setState(State state) {
this.state = state;
}
public Model getModel() {
return this.model;
}
public void setModel(Model model) {
this.model = model;
}
public City getCity() {
return this.city;
}
public void setCity(City city) {
this.city = city;
}
public String getVin() {
return this.vin;
}
public void setVin(String vin) {
this.vin = vin;
}
public int getYear() {
return this.year;
}
public void setYear(int year) {
this.year = year;
}
public String getImage() {
return this.image;
}
public void setImage(String image) {
this.image = image;
}
public String getEngine() {
return this.engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
public String getTrans() {
return this.trans;
}
public void setTrans(String trans) {
this.trans = trans;
}
public String getMileage() {
return this.mileage;
}
public void setMileage(String mileage) {
this.mileage = mileage;
}
public BigDecimal getPrice() {
return this.price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public String getColor() {
return this.color;
}
public void setColor(String color) {
this.color = color;
}
public BigDecimal getHwyMpg() {
return this.hwyMpg;
}
public void setHwyMpg(BigDecimal hwyMpg) {
this.hwyMpg = hwyMpg;
}
public BigDecimal getCityMpg() {
return this.cityMpg;
}
public void setCityMpg(BigDecimal cityMpg) {
this.cityMpg = cityMpg;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAccessories() {
return this.accessories;
}
public void setAccessories(String accessories) {
this.accessories = accessories;
}
public String getComments() {
return this.comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public Set getBuyers() {
return this.buyers;
}
public void setBuyers(Set buyers) {
this.buyers = buyers;
}
public Set getOwnerships() {
return this.ownerships;
}
public void setOwnerships(Set ownerships) {
this.ownerships = ownerships;
}
public Set getCarSearchLists() {
return this.carSearchLists;
}
public void setCarSearchLists(Set carSearchLists) {
this.carSearchLists = carSearchLists;
}
}
Model:
package com.usedcarsearch.domain;
// Generated Feb 19, 2013 10:31:37 PM by Hibernate Tools 3.4.0.CR1
import java.util.HashSet;
import java.util.Set;
/**
* Model generated by hbm2java
*/
public class Model implements java.io.Serializable {
private Integer modId;
private String model;
private Set cars = new HashSet(0);
public Model() {
}
public Model(String model) {
this.model = model;
}
public Model(String model, Set cars) {
this.model = model;
this.cars = cars;
}
public Integer getModId() {
return this.modId;
}
public void setModId(Integer modId) {
this.modId = modId;
}
public String getModel() {
return this.model;
}
public void setModel(String model) {
this.model = model;
}
public Set getCars() {
return this.cars;
}
public void setCars(Set cars) {
this.cars = cars;
}
}
Car.hbm
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/
Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net
/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 19, 2013 10:31:37 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping default-lazy="false">
<class name="com.usedcarsearch.domain.Car" table="Car">
<id name="carId" type="java.lang.Integer">
<column name="carId"/>
<generator class="identity"/>
</id>
<many-to-one class="com.usedcarsearch.domain.Make"
fetch="select" name="make">
<column name="fkMakeId" not-null="true"/>
</many-to-one>
<many-to-one class="com.usedcarsearch.domain.State"
fetch="select" name="state">
<column name="fkStateId" not-null="true"/>
</many-to-one>
<many-to-one class="com.usedcarsearch.domain.Model"
fetch="select" name="model">
<column name="fkModelId" not-null="true"/>
</many-to-one>
<many-to-one class="com.usedcarsearch.domain.City"
fetch="select" name="city">
<column name="fkZipCode" not-null="true"/>
</many-to-one>
<property generated="never" lazy="false" name="vin" type="string">
<column length="25" name="vin"/>
</property>
<property generated="never" lazy="false" name="year" type="int">
<column name="year" not-null="true"/>
</property>
<property generated="never" lazy="false" name="image" type="string">
<column length="100" name="image"/>
</property>
<property generated="never" lazy="false" name="engine" type="string">
<column length="45" name="engine" not-null="true"/>
</property>
<property generated="never" lazy="false" name="trans" type="string">
<column length="45" name="trans" not-null="true"/>
</property>
<property generated="never" lazy="false" name="mileage" type="string">
<column length="20" name="mileage" not-null="true"/>
</property>
<property generated="never" lazy="false" name="price" type="big_decimal">
<column name="price" precision="11"/>
</property>
<property generated="never" lazy="false" name="color" type="string">
<column length="20" name="color"/>
</property>
<property generated="never" lazy="false" name="hwyMpg" type="big_decimal">
<column name="hwyMpg" precision="3" scale="1"/>
</property>
<property generated="never" lazy="false" name="cityMpg" type="big_decimal">
<column name="cityMpg" precision="3" scale="1"/>
</property>
<property generated="never" lazy="false" name="address" type="string">
<column length="50" name="address"/>
</property>
<property generated="never" lazy="false" name="accessories" type="string">
<column length="100" name="accessories"/>
</property>
<property generated="never" lazy="false" name="comments" type="string">
<column length="100" name="comments"/>
</property>
<set fetch="select" inverse="true" lazy="false" name="buyers"
sort="unsorted" table="Buyer">
<key>
<column name="fkCarId" not-null="true"/>
</key>
<one-to-many class="com.usedcarsearch.domain.Buyer"/>
</set>
<set fetch="select" inverse="true" lazy="false" name="ownerships"
sort="unsorted" table="Ownership">
<key>
<column name="fkCarId" not-null="true"/>
</key>
<one-to-many class="com.usedcarsearch.domain.Ownership"/>
</set>
<set fetch="select" inverse="true" lazy="false" name="carSearchLists"
sort="unsorted" table="Car_Search_List">
<key>
<column name="fkCarId" not-null="true"/>
</key>
<one-to-many class="com.usedcarsearch.domain.CarSearchList"/>
</set>
</class>
</hibernate-mapping>
JSP code: (I did not include all but only what is necessary)
<spring:bind path="command.model">
<select name='<c:out value="${status.expression}"/>'>
<option value=""></option>
<c:forEach items="${cars}" var="car">
<option value='<c:out value="${car.model.modId}"/>'
<c:if test="${car.model.model == status.value.model}">SELECTED</c:if>>
<c:out value="${car.model.model}" />
</option>
</c:forEach>
</select>
I got it fixed. It seems I had to add the foreign key property in the Car.hbm along with adding the foreign keys variables, getters and setters to Car.
Based on the error message you might want to change line 28 from:
status.value.model
to
status.value
Related
I am new to hibernate. My goal is to get query results to an ArrayList but I keep getting the ERROR: HHH000091: Expected type: int, actual value: org.hibernate.collection.internal.PersistentSet
I strongly believe the problem is in my mapping, any help is appreciated.
Errors
ERROR: HHH000123: IllegalArgumentException in class: com.mycompany.mavenproject1.Medicamento, setter method of property: labJoin
Apr 07, 2017 10:23:10 AM org.hibernate.property.BasicPropertyAccessor$BasicSetter set
ERROR: HHH000091: Expected type: int, actual value: org.hibernate.collection.internal.PersistentSet
My HQL query:
from Medicamento as m left join from Laboratorio as l where l.codigoLab=m.codigoLab
Mapping XML:
<class name="com.mycompany.mavenproject1.Medicamento" table="MEDICAMENTO" schema="ADMINFARMACIA" optimistic-lock="version">
<id name="codigoMed" type="long">
<column name="CODIGO_MED" not-null="true"/>
<generator class="assigned" />
</id>
<property name="nombreComercial" type="string">
<column name="NOMBRE_COMERCIAL" length="100" not-null="true" />
</property>
<property name="codigoPrincipio" type="string">
<column name="CODIGO_PRINCIPIO" length="50" not-null="true" />
</property>
<property name="stockMinimo" type="java.lang.Integer">
<column name="STOCK_MINIMO" />
</property>
<property name="codigoLab" type="java.lang.Integer">
<column name="CODIGO_LAB" />
</property>
<property name="comentario" type="string">
<column name="COMENTARIO" length="1000" />
</property>
<property name="existencias" type="java.lang.Integer">
<column name="EXISTENCIAS"/>
</property>
<set name="labJoin" fetch="join">
<key column="CODIGO_LAB" />
<one-to-many class="com.mycompany.mavenproject1.Laboratorio"/>
</set>
</class>
Laboratorio.java:
public class Laboratorio implements java.io.Serializable{
private int codigoLab;
private String nombreLab;
public Laboratorio(){
}
public int getCodigoLab() {
return codigoLab;
}
public void setCodigoLab(int codigoLab) {
this.codigoLab = codigoLab;
}
public String getNombreLab() {
return nombreLab;
}
public void setNombreLab(String nombreLab) {
this.nombreLab = nombreLab;
}
}
Medicamento.java - in this one labJoin and labName are the fields on "Laboratorio" table
public class Medicamento implements java.io.Serializable{
private Long codigoMed;
private String nombreComercial;
private String codigoPrincipio;
private int stockMinimo;
private int codigoLab;
private String comentario;
private int existencias;
private int labJoin;
private String labName;
public Medicamento(){
}
public Long getCodigoMed() {
return codigoMed;
}
public void setCodigoMed(Long codigoMed) {
this.codigoMed = codigoMed;
}
public String getNombreComercial() {
return nombreComercial;
}
public void setNombreComercial(String nombreComercial) {
this.nombreComercial = nombreComercial;
}
public String getCodigoPrincipio() {
return codigoPrincipio;
}
public void setCodigoPrincipio(String codigoPrincipio) {
this.codigoPrincipio = codigoPrincipio;
}
public int getStockMinimo() {
return stockMinimo;
}
public void setStockMinimo(int stockMinimo) {
this.stockMinimo = stockMinimo;
}
public int getCodigoLab() {
return codigoLab;
}
public void setCodigoLab(int codigoLab) {
this.codigoLab = codigoLab;
}
public int getLabJoin() {
return labJoin;
}
public void setLabJoin(int labJoin) {
this.labJoin = labJoin;
}
public String getLabName() {
return labName;
}
public void setLabJoin(String labName) {
this.labName= labName;
}
public String getComentario() {
return comentario;
}
public void setComentario(String comentario) {
this.comentario = comentario;
}
public int getExistencias() {
return existencias;
}
public void setExistencias(int existencias) {
this.existencias = existencias;
}
}
Calling method
private void displayResult(List rl){
ArrayList<Object> oneRow = new ArrayList<Object>();
for (Object o: rl){
Medicamento medList = (Medicamento)o;
System.out.println(labList.getCodigoLab());
oneRow.add(medList.getCodigoMed());
oneRow.add(medList.getExistencias());
oneRow.add(medList.getNombreComercial());
oneRow.add(medList.getLabJoin());
}
LowStockTableModel model = new LowStockTableModel(oneRow);
try{
jTable1.setModel(model);
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e.getLocalizedMessage(),"Error",JOptionPane.ERROR_MESSAGE);
jTextField1.setText("");
}
}
In your mapping, you have this
<set name="labJoin" fetch="join">
<key column="CODIGO_LAB" />
<one-to-many class="com.mycompany.mavenproject1.Laboratorio"/>
</set>
But property labJoin has type int. The type of this property has to be Set<Laboratorio>.
Situation: I have 2 classes with relation oneToMany (Category oneToMany Product),
when creating Product like this:
Category c1 = new Category("Printer");
Category c2 = new Category("Scanner");
Category c3 = new Category("Phone");
Factory.getInstance().getCategoryDAO().addCategory(c1);//id1
Factory.getInstance().getCategoryDAO().addCategory(c2);//id2
Factory.getInstance().getCategoryDAO().addCategory(c3);//id3
//Product(catID, name, price)
Product p1 = new Product(2,"Panasonic", new BigDecimal("322.12"));
Product p2 = new Product(2,"Samsung", new BigDecimal("700.01"));
Product p3 = new Product(3,"NOKIA", new BigDecimal("12000.12"));
Factory.getInstance().getProductDAO().addProduct(p1);
Factory.getInstance().getProductDAO().addProduct(p2);
Factory.getInstance().getProductDAO().addProduct(p3);
I came into this exception:
Hibernate: insert into categories (CATEGORY_ID, CATEGORY_NAME) values (null, ?)
Hibernate: insert into categories (CATEGORY_ID, CATEGORY_NAME) values (null, ?)
Hibernate: insert into categories (CATEGORY_ID, CATEGORY_NAME) values (null, ?)
авг 22, 2016 12:06:21 AM org.hibernate.property.BasicPropertyAccessor$BasicGetter get
ERROR: HHH000122: IllegalArgumentException in class: model.Category, getter method of property: id
авг 22, 2016 12:06:36 AM org.hibernate.property.BasicPropertyAccessor$BasicGetter get
ERROR: HHH000122: IllegalArgumentException in class: model.Category, getter method of property: id
авг 22, 2016 12:06:37 AM org.hibernate.property.BasicPropertyAccessor$BasicGetter get
ERROR: HHH000122: IllegalArgumentException in class: model.Category, getter method of property: id
Class Category:
public class Category {
private Integer id;
private String name;
private Set<Product> products;
public Category(String name) {
this.name = name;
this.products = new HashSet<Product>(0);
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Set<Product> getProducts() {
return products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Class Product :
public class Product {
private Integer id;
private Integer catId;
private String name;
private BigDecimal price;
public Product(Integer catId, String name, BigDecimal price) {
this.catId = catId;
this.name = name;
this.price = price;
}
public Integer getId() {
return id;
}
public Integer getCatId() {
return catId;
}
public String getName() {
return name;
}
public BigDecimal getPrice() {
return price;
}
public void setId(Integer id) {
this.id = id;
}
public void setCatId(Integer catId) {
this.catId = catId;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
}
And Category mapping:
<class name="model.Category" table="categories">
<id name="id" type="java.lang.Integer">
<column name="CATEGORY_ID"/>
<generator class="identity"/>
</id>
<property name="name" type="java.lang.String">
<column name="CATEGORY_NAME" not-null="true" unique="true"/>
</property>
<set name="products" table="products" lazy="false" inverse="true" fetch="select">
<key>
<column name="CAT_ID" not-null="true"/>
</key>
<one-to-many class="model.Product"/>
</set>
</class>
And Product mapping:
<hibernate-mapping>
<class name="model.Product" table="products">
<id name="id" type="java.lang.Integer">
<column name="PRODUCT_ID"/>
<generator class="identity"/>
</id>
<property name="name" type="java.lang.String">
<column name="PRODUCT_NAME"/>
</property>
<property name="price" type="java.math.BigDecimal" precision="2" scale="16" >
<column name="PRODUCT_PRICE"/>
</property>
<many-to-one name="catId" class="model.Category" fetch="select">
<column name="CAT_ID" not-null="true"/>
</many-to-one>
</class>
I found solution by myself.
I replaced private Integer catId; with private Category category; in Product class.
so I want to get data from my DB, but I always get this exception, saying that my getAll() method is returning null. I don't know what to do, can anyone help me?
Thanks
WarehouseHasProduct.java
public class WarehouseHasProduct implements java.io.Serializable {
private Integer id;
private Product product = new Product();
private Warehouse warehouse = new Warehouse();
private int stock;
public WarehouseHasProduct() {
}
public WarehouseHasProduct(Product product, Warehouse warehouse, int stock) {
this.product = product;
this.warehouse = warehouse;
this.stock = stock;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Product getProduct() {
return this.product;
}
public void setProduct(Product product) {
this.product = product;
}
public Warehouse getWarehouse() {
return this.warehouse;
}
public void setWarehouse(Warehouse warehouse) {
this.warehouse = warehouse;
}
public int getStock() {
return this.stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
Warehouse.java
public class Warehouse implements java.io.Serializable {
private Integer id;
private String description;
private Set<WarehouseHasProduct> warehouseHasProducts = new HashSet<WarehouseHasProduct>(
0);
public Warehouse() {
}
public Warehouse(String description) {
this.description = description;
}
public Warehouse(String description,
Set<WarehouseHasProduct> warehouseHasProducts) {
this.description = description;
this.warehouseHasProducts = warehouseHasProducts;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<WarehouseHasProduct> getWarehouseHasProducts() {
return this.warehouseHasProducts;
}
public void setWarehouseHasProducts(
Set<WarehouseHasProduct> warehouseHasProducts) {
this.warehouseHasProducts = warehouseHasProducts;
}
}
Product.java
public class Product implements java.io.Serializable {
private Integer id;
private ProductType productType;
private String productName;
private String description;
private double price;
private Set<WarehouseHasProduct> warehouseHasProducts = new HashSet<WarehouseHasProduct>(
0);
private Set<Order> orders = new HashSet<Order>(0);
public Product() {
}
public Product(ProductType productType, String productName,
String description, double price) {
this.productType = productType;
this.productName = productName;
this.description = description;
this.price = price;
}
public Product(ProductType productType, String productName,
String description, double price,
Set<WarehouseHasProduct> warehouseHasProducts, Set<Order> orders) {
this.productType = productType;
this.productName = productName;
this.description = description;
this.price = price;
this.warehouseHasProducts = warehouseHasProducts;
this.orders = orders;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public ProductType getProductType() {
return this.productType;
}
public void setProductType(ProductType productType) {
this.productType = productType;
}
public String getProductName() {
return this.productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public Set<WarehouseHasProduct> getWarehouseHasProducts() {
return this.warehouseHasProducts;
}
public void setWarehouseHasProducts(
Set<WarehouseHasProduct> warehouseHasProducts) {
this.warehouseHasProducts = warehouseHasProducts;
}
public Set<Order> getOrders() {
return this.orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
}
WarehouseHasProduct.hbm.xml
<hibernate-mapping>
<class name="com.app.hibernate.data.WarehouseHasProduct" table="warehouse_has_product" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="product" class="com.app.hibernate.data.Product" fetch="select">
<column name="product_id" not-null="true" />
</many-to-one>
<many-to-one name="warehouse" class="com.app.hibernate.data.Warehouse" fetch="select">
<column name="warehouse_id" not-null="true" />
</many-to-one>
<property name="stock" type="int">
<column name="stock" not-null="true" />
</property>
</class>
</hibernate-mapping>
Warehouse.hbm.xml
<hibernate-mapping>
<class name="com.app.hibernate.data.Warehouse" table="warehouse" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="description" type="string">
<column name="description" length="100" not-null="true" />
</property>
<set name="warehouseHasProducts" table="warehouse_has_product" inverse="true" lazy="true" fetch="select">
<key>
<column name="warehouse_id" not-null="true" />
</key>
<one-to-many class="com.app.hibernate.data.WarehouseHasProduct" />
</set>
</class>
</hibernate-mapping>
Product.hbm.xml
<hibernate-mapping>
<class name="com.app.hibernate.data.Product" table="product" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="productType" class="com.app.hibernate.data.ProductType" fetch="select">
<column name="type_id" not-null="true" />
</many-to-one>
<property name="productName" type="string">
<column name="productName" length="100" not-null="true" />
</property>
<property name="description" type="string">
<column name="description" length="100" not-null="true" />
</property>
<property name="price" type="double">
<column name="price" precision="22" scale="0" not-null="true" />
</property>
<set name="warehouseHasProducts" table="warehouse_has_product" inverse="true" lazy="true" fetch="select">
<key>
<column name="product_id" not-null="true" />
</key>
<one-to-many class="com.app.hibernate.data.WarehouseHasProduct" />
</set>
<set name="orders" table="order_has_product" inverse="false" lazy="true" fetch="select">
<key>
<column name="product_id" not-null="true" />
</key>
<many-to-many entity-name="com.app.hibernate.data.Order">
<column name="order_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
WarehouseHasProductService.java
#Component
public class WarehouseHasProductService {
#Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
#Transactional
public void register(WarehouseHasProduct whp){
// Acquire session
Session session = sessionFactory.getCurrentSession();
// Save employee, saving behavior get done in a transactional manner
session.save(whp);
}
#SuppressWarnings("unchecked")
#Transactional
public List<WarehouseHasProduct> getAll(){
// Acquire session
Session session = sessionFactory.getCurrentSession();
List list = session.createQuery("from com.app.hibernate.data.WarehouseHasProduct").list();
return list;
}
}
RegisterWarehouseHasProduct.java
#ManagedBean
#SessionScoped
public class RegisterWarehouseHasProduct{
#ManagedProperty("#{whpService}")
private WarehouseHasProductService whpService;
private WarehouseHasProduct whp = new WarehouseHasProduct();
public WarehouseHasProductService getWhpService() {
return whpService;
}
public void setWhpService(WarehouseHasProductService whpService) {
this.whpService = whpService;
}
public WarehouseHasProduct getWhp() {
return whp;
}
public void setWhp(WarehouseHasProduct whp) {
this.whp = whp;
}
public String register() {
// Calling Business Service
whpService.register(whp);
// Add message
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("The Product "+whp.getId()+" Is Registered Successfully"));
return "";
}
public List<WarehouseHasProduct> getAll(){
return whpService.getAll();
}
}
Home.xhtml
<p:dataTable var="p" value="#{registerProduct.getAllProducts()}">
<f:facet name="header">
Products List
</f:facet>
<p:column headerText="Code">
<h:outputText value="#{p.id}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{p.productName}" />
</p:column>
<p:column headerText="Description">
<h:outputText value="#{p.description}" />
</p:column>
<p:column headerText="Type">
<h:outputText value="#{p.productType.description}" />
</p:column>
<p:column headerText="Price">
<h:outputText value="#{p.price}" />
</p:column>
</p:dataTable>
I really appreciate an answer. Thank you!
I am new to hibernate. I used MYSQL database. there is date field in my table. its type is DATE. There is a value 1989-08-13. When I get value using hibernate it gives that date as 6189498000000. I want to get value as real date (1989-08-13). Please help me
This is my xml file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 27, 2013 1:03:09 AM by Hibernate Tools 4.0.0 -->
<hibernate-mapping>
<class name="core.classes.Staff" table="staff" catalog="surgercare">
<id name="staffId" type="int">
<column name="staff_ID" />
<generator class="assigned" />
</id>
<property name="staffName" type="string">
<column name="staff_Name" length="150" />
</property>
<property name="staffDesignation" type="string">
<column name="staff_designation" length="50" />
</property>
<property name="createDate" type="timestamp">
<column name="CreateDate" length="19" />
</property>
<property name="createUser" type="string">
<column name="CreateUser" length="200" />
</property>
<property name="lastUpDate" type="timestamp">
<column name="LastUpDate" length="19" />
</property>
<property name="lastUpDateUser" type="string">
<column name="LastUpDateUser" length="200" />
</property>
</class>
</hibernate-mapping>
My class file
import java.util.Date;
public class Staff implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int staffId;
private String staffName;
private String staffDesignation;
private Date createDate;
private String createUser;
private Date lastUpDate;
private String lastUpDateUser;
public Staff() {
}
public Staff(int staffId) {
this.staffId = staffId;
}
public Staff(int staffId, String staffName, String staffDesignation,
Date createDate, String createUser, Date lastUpDate,
String lastUpDateUser) {
this.staffId = staffId;
this.staffName = staffName;
this.staffDesignation = staffDesignation;
this.createDate = createDate;
this.createUser = createUser;
this.lastUpDate = lastUpDate;
this.lastUpDateUser = lastUpDateUser;
}
public int getStaffId() {
return this.staffId;
}
public void setStaffId(int staffId) {
this.staffId = staffId;
}
public String getStaffName() {
return this.staffName;
}
public void setStaffName(String staffName) {
this.staffName = staffName;
}
public String getStaffDesignation() {
return this.staffDesignation;
}
public void setStaffDesignation(String staffDesignation) {
this.staffDesignation = staffDesignation;
}
public Date getCreateDate() {
return this.createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getCreateUser() {
return this.createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
public Date getLastUpDate() {
return this.lastUpDate;
}
public void setLastUpDate(Date lastUpDate) {
this.lastUpDate = lastUpDate;
}
public String getLastUpDateUser() {
return this.lastUpDateUser;
}
public void setLastUpDateUser(String lastUpDateUser) {
this.lastUpDateUser = lastUpDateUser;
}
}
this is my controller
tx = ses.beginTransaction();
Query query = ses.createQuery("select s from Staff as s where s.staffId = :ID");
query.setString("ID", docID);
List<Staff> DocDetailsList = castlist(Staff.class,query.list());
tx.commit();
return DocDetailsList;
In your hbm file, there are "timestamp" column type. Let hibernate guess the column type:
<property name="createDate" column="CreateDate" />
In other words, if you have the following property:
java.util.Date createDate;
hibernate3-maven-plugin hibernate3:hbm2ddl goal will create the following column in MySql:
`CreateDate` datetime DEFAULT NULL
Note: datetime and timestamp have different ranges. Same for Java Long (timestamp) and java.util.Date.
i am new in using hibernate and for some reason i am getting a list of null objects when i am using the following code:
public static void main(String args[])
{
Session s = HibernateUtil.currentSession();
ArrayList lst = (ArrayList) s.createQuery("from Users").list();
for(Object obj : lst){
Users user = (Users)obj;
System.Out.println(user.getUserid()); // null
}
}
my hibernate mapping xml looks like this:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 27, 2012 9:48:45 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="us.Users" table="USERS">
<id name="userid" type="int">
<column name="USERID" precision="9" scale="0" />
<generator class="assigned" />
</id>
<property name="username" type="string">
<column name="USERNAME" length="200" />
</property>
<property name="password" type="string">
<column name="PASSWORD" length="200" />
</property>
<property name="firstName" type="string">
<column name="FIRST_NAME" length="200" />
</property>
<property name="lastName" type="string">
<column name="LAST_NAME" length="200" />
</property>
<property name="dateOfBirth" type="timestamp"> // my guess was that the problem appears with the timestamp property
<column name="DATE_OF_BIRTH" />
</property>
<property name="registrationDate" type="timestamp">
<column name="REGISTRATION_DATE" />
</property>
<one-to-one name="administrators" class="assignment2.Administrators"></one-to-one>
<set name="histories" table="HISTORY" inverse="true" lazy="false" fetch="select">
<key>
<column name="USERID" precision="9" scale="0" not-null="true" />
</key>
<one-to-many class="us.History" />
</set>
<set name="loginlogs" table="LOGINLOG" inverse="true" lazy="false" fetch="select">
<key>
<column name="USERID" precision="9" scale="0" not-null="true" />
</key>
<one-to-many class="us.Loginlog" />
</set>
</class>
here is my hibernate.cfg.xml :
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">abcd</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#localhost:1521:xe</property>
<property name="hibernate.connection.username">SYSTEM</property>
<property name="hibernate.default_schema">SYSTEM</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<mapping resource="assignment2/History.hbm.xml" />
<mapping resource="assignment2/Similarity.hbm.xml" />
<mapping resource="assignment2/Loginlog.hbm.xml" />
<mapping resource="assignment2/Users.hbm.xml" />
<mapping resource="assignment2/Administrators.hbm.xml" />
<mapping resource="assignment2/Mediaitems.hbm.xml" />
</session-factory>
and the users class:
/**
* Users generated by hbm2java
*/
public class Users implements java.io.Serializable {
private int userid;
private String username;
private String password;
private String firstName;
private String lastName;
private Date dateOfBirth;
private Date registrationDate;
private Administrators administrators;
private Set<History> histories = new HashSet<History>(0);
private Set<Loginlog> loginlogs = new HashSet<Loginlog>(0);
public Users() {
}
public Users(int userid) {
this.userid = userid;
}
public Users(int userid, String username, String password,
String firstName, String lastName, Serializable dateOfBirth,
Serializable registrationDate, Administrators administrators,
Set<History> histories, Set<Loginlog> loginlogs) {
this.userid = userid;
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
this.registrationDate = registrationDate;
this.administrators = administrators;
this.histories = histories;
this.loginlogs = loginlogs;
}
public int getUserid() {
return this.userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getDateOfBirth() {
return this.dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public Date getRegistrationDate() {
return this.registrationDate;
}
public void setRegistrationDate(Date registrationDate) {
this.registrationDate = registrationDate;
}
public Administrators getAdministrators() {
return this.administrators;
}
public void setAdministrators(Administrators administrators) {
this.administrators = administrators;
}
public Set<History> getHistories() {
return this.histories;
}
public void setHistories(Set<History> histories) {
this.histories = histories;
}
public Set<Loginlog> getLoginlogs() {
return this.loginlogs;
}
public void setLoginlogs(Set<Loginlog> loginlogs) {
this.loginlogs = loginlogs;
}
}
thanks alot in advance