Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
Hello!
I have a strage problem here: I'm following the hibernate manual reference on chapter 1 where it says to Work with associations. So the code applied to my aplication context looks very like the same! Well the prob i'm facing is when I load the class Product:
Product theProduct = (Product) session.load(Product.class, prodId); I get a NullPointerException. I have no idea how this came up, so I'd really apprecite some here on this.
Here the class Manager code:
NOTE: the db schema on this part is: TableProduct (1)----(*)IntermTableProductmparameter(*)----(1)TableMonitParameter
NOTE2: I generated the java classes with the last version of HibernateTools. (DB schema ->xml.hbm->hbm2java)
public class ProductManager {
public static void main(String[] args) {
ProductManager mgr = new ProductManager();
if (args[0].equals("inserir_produto")) {
mgr.createAndStoreProduct("PRD-03", "My Product3", "Steel");
}
else if (args[0].equals("listar")) {
List products = mgr.listProducts();
for (int i = 0; i < products.size(); i++) {
Product myProduct = (Product) products.get(i);
System.out.println("Product: " + myProduct.getProductId() +
" Name: " + myProduct.getName() + " Classification: " +
myProduct.getClassification());
}
}
else if (args[0].equals("addprodtoprodmparam")) {
Integer MonitParam = mgr.createAndStoreMonitorizationParameter
(4, "Motor Temperature", "Temperature of the motor", "Celcius", 90.0, 130.0, 50.0,
120.0, 60.0, 110.0, 70.0, 5.0, 1.0, 1.0, 10.0, 10.0);
String productId = mgr.createAndStoreProduct("PRD-04", "My Product4", "Plastic");
mgr.addProductToProductmparameter(productId, MonitParam);
System.out.println("Added product " + productId + " to productmparameter " + MonitParam);
}
HibernateUtil.getSessionFactory().close();
}
private String createAndStoreProduct(String prodId, String name, String classification) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try{
session.beginTransaction();
Product myProduct = new Product();
myProduct.setProductId(prodId);
myProduct.setName(name);
myProduct.setClassification(classification);
session.save(myProduct);
session.getTransaction().commit();
return myProduct.getProductId();
}
catch (RuntimeException e){
session.getTransaction().rollback();
System.err.println("Could not create and store product: " + e);
return null;
}
}
private Integer createAndStoreMonitorizationParameter
(Integer ParameterId, String name, String description, String measurementUnit, double avrgValue,
double maxValue, double minValue, double maxLimitAlarm,
double minLimitAlarm, double maxLimitWarning, double minLimitWarning,
double sampleInterval, double stsampleInterval, double stpredictionInterval,
double ltsampleInterval, double ltpredictionInterval) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try{
session.beginTransaction();
Monitorizationparameter myMonitorizationparam = new Monitorizationparameter();
myMonitorizationparam.setParameterId(ParameterId);
myMonitorizationparam.setName(name);
myMonitorizationparam.setDescription(description);
myMonitorizationparam.setMeasurementUnit(measurementUnit);
myMonitorizationparam.setAvrgValue(avrgValue);
myMonitorizationparam.setMaxValue(maxValue);
myMonitorizationparam.setMinValue(minValue);
myMonitorizationparam.setMaxLimitAlarm(maxLimitAlarm);
myMonitorizationparam.setMinLimitAlarm(minLimitAlarm);
myMonitorizationparam.setMaxLimitWarning(maxLimitWarning);
myMonitorizationparam.setMinLimitWarning(minLimitWarning);
myMonitorizationparam.setSampleInterval(sampleInterval);
myMonitorizationparam.setStsampleInterval(stsampleInterval);
myMonitorizationparam.setStpredictionInterval(stpredictionInterval);
myMonitorizationparam.setLtsampleInterval(ltsampleInterval);
myMonitorizationparam.setLtpredictionInterval(ltpredictionInterval);
//myMonitorizationparam.setMaintenanceobject(maintenanceobject);
//myMonitorizationparam.setSensor(sensor);
session.save(myMonitorizationparam);
session.getTransaction().commit();
return myMonitorizationparam.getParameterId();
}
catch (RuntimeException e){
session.getTransaction().rollback();
System.err.println("Could not create and store product: " + e);
return null;
}
}
private void addProductToProductmparameter(String prodId, Integer paramId) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Product theProduct = (Product) session.load(Product.class, prodId);
Productmparameter prdmparam =
(Productmparameter) session.load(Productmparameter.class, paramId);
theProduct.getProductmparameters().add(prdmparam);
session.getTransaction().commit();
}
}