p3ace p3ace - 1 year ago 84
Java Question

session.get() unable to resolve field,argument

I'm totally new to Spring MVC but still trying to understand its methods and its way of referencing things. There's a video tutorial course I'm following.

I'm trying to implement a Model through a class.


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

public class ProductDaoImpl implements ProductDao {

private SessionFactory sessionFactory;

public void addProduct(Product product) {
Session session = sessionFactory.getCurrentSession();

public Product getProductById(String id) {
Session session = sessionFactory.getCurrentSession();
Product product = (Product) session.get(Product.class.productId);
// unable to resolve the productId on get()
return product;

public List<Product> getAllProducts() {
return null;

public void deleteProduct(String id) {



import java.util.List;

public interface ProductDao {
void addProduct(Product product);
Product getProductById(String id);
List<Product> getAllProducts();
void deleteProduct(String id);

Product.java Model

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class Product {

@GeneratedValue(strategy = GenerationType.AUTO) //tells the system that when an instance is put to database, it will be numbered automatically starting from 1
private String productName;
private String productCategory;
private String productDescription;
private double productPrice;
private String productDimension;
private String productStatus;
private int unitInStock;
private String productManufacturer;
private String productId;

However, it's unable to resolve the productId argument in

public Product getProductById(String id) {
Session session = sessionFactory.getCurrentSession();
Product product = (Product) session.get(Product.class.productId);
return product;

method able to access the fields of
Is it asking for a field in

I don't understand why it can't resolve the

I hope you can help.


Answer Source

The expression Product.class.productId doesn't make sense in Java. Product.class is a class literal, meaning that it's a constant value that represents the Product class, an instance of java.lang.Class. Class has no productId field.

Additionally, you should read the Javadoc for the method--it takes two parameters, a Class object (to tell it what sort of thing you're getting) and an ID. Your call should therefore be session.get(Product.class, id), and if you're using Hibernate 5, you don't need to cast to a Product.

All of this is rather moot, however--instead of hand-rolling a DAO targeting Hibernate, use JPA (which provides advantages such as a generic API, eliminating the need for casting) and Spring Data (which will autogenerate this entire DAO for you from nothing but an empty interface declaration). Additionally, you're dealing with topics that can get complicated, and you would do well to go through some exercises to learn core Java before tackling something like ORM.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download