'Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value : Product.Name
I have a main class where error(at Connect.main(Connect.java:53))
public class Connect {
public static void main(String[] args) throws SQLException {
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").
addAnnotatedClass(Product.class).
addAnnotatedClass(Customer.class).
addAnnotatedClass(CustomerHistory.class).
buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
//Drop tablets
Query Queri = session.createQuery("DELETE CustomerHistory");
Queri.executeUpdate();
Queri = session.createQuery("DELETE Customer");
Queri.executeUpdate();
Queri = session.createQuery("DELETE Product");
Queri.executeUpdate();
//Add customers
String[] Customers = {"Лис","Пес","Кот"};
for(int i = 1; i <= Customers.length;i++){
Customer customer = new Customer();
customer.setId(i);
customer.setName(Customers[i-1]);
session.save(customer);
}
//Add products and buy
HashMap<String, Integer> products = new HashMap<String, Integer>();
products.put("Картошка", 50);
products.put("Мюсли", 30);
products.put("Пепса", 20);
int b = 1;
for(String key : products.keySet()){
Product product = new Product();
product.setId(b);
product.setName(key);
product.setPrice(products.get(key));
session.save(product);
CustomerHistory history = new CustomerHistory();
history.setId(b);
history.setName(Customers[b-1]);
history.setNameProduct(key);
session.save(history);
b++;
}
session.getTransaction().commit();
//Scanner
Scanner scanner = new Scanner(System.in);
int command;
do{
System.out.println("Введите цифру для запроса");
System.out.println("1 - посмотреть какие товары покупал клиент");
System.out.println("2 - какие клиенты купили определенный товар");
System.out.println("3 - покупка товара");
System.out.println("4 - удалить из базы покупателя");
System.out.println("5 - выход");
System.out.println("6 - сколько стоил товар в момент покупки клиентом");
command = Integer.parseInt(scanner.nextLine());
switch (command){
case 1:
session.beginTransaction();
System.out.println("Введите имя клиента");
Queri = session.createQuery("from CustomerHistory WHERE Name = :x");
Queri.setParameter("x",scanner.nextLine());
List<CustomerHistory> cus = Queri.list();
for(CustomerHistory s : cus)
{ System.out.println("id="+s.getId() + " Name="+s.getName()+" NameProduct="+s.getNameProduct());}
session.getTransaction().commit();
break;
case 2:
session.beginTransaction();
System.out.println("Введите имя товара");
Queri = session.createQuery("from CustomerHistory WHERE NameProduct = :x");
Queri.setParameter( "x", scanner.nextLine());
cus = Queri.list();
for(CustomerHistory s : cus)
{ System.out.println("id="+s.getId() + " Name="+s.getName()+" NameProduct="+s.getNameProduct());}
session.getTransaction().commit();
break;
case 3:
session.beginTransaction();
System.out.println("Введите имя человека и товар, который он покупает");
Queri = session.createNativeQuery("INSERT INTO CustomerHistory (id, Name, NameProduct) VALUES (:x,:y,:z)");
Queri.setParameter( "x", b+1) ;
Queri.setParameter( "y", scanner.nextLine()) ;
Queri.setParameter( "z", scanner.nextLine()) ;
Queri.executeUpdate();
session.getTransaction().commit();
break;
case 4:
session.beginTransaction();
System.out.println("Введите имя клиента для удаления");
Queri = session.createQuery("DELETE Customer WHERE Name = :x");
Queri.setParameter( "x", scanner.nextLine());
Queri.executeUpdate();
session.getTransaction().commit();
break;
case 6:
session.beginTransaction();
Queri = session.createQuery("select g1.id,g1.Name,g1.NameProduct,g2.Price from CustomerHistory as g1 inner join Product as g2 on g1.id = g2.id");
List<Object[]> sus = (List<Object[]>) Queri.list();
for(Object[] x : sus)
{ System.out.println("id="+x[0]+ " Покупатель=" + x[1] + " Продукт=" +x[2] + " Цена=" +x[3]);}
session.getTransaction().commit();
break;
default:
System.out.println("Завершение программы");
}
}
while (command != 5);
}
}
Class Customer
@Entity
public class Customer {
@Id
@Column (name = "id", nullable = false)
private int id;
public int getId(){return id;}
public void setId(int id){this.id = id;}
@Column (name = "Name", nullable = false)
private String Name;
public String getName(){return Name;}
public void setName(String name){ Name = name;}
}
Class Product where does the error possibly come from
@Entity
public class Product {
@Id
@Column(name = "id",nullable = false)
private int id;
public int getId(){return id;}
public void setId(int id){this.id = id;}
@Column(name = "Name", nullable = false)
private String Name;
public String getName(){return Name;}
public void setName(String Name){Name = Name;}
@Column(name = "Price",nullable = false)
private int Price;
public int getPrice(){return Price;}
public void setPrice(int price){this.Price = price;}
}
And last class CustomerHistory its all buy history
@Entity
public class CustomerHistory {
@Id
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "Name", nullable = false)
private String Name;
public String getName(){return Name;}
public void setName(String name){this.Name = name;}
@Column(name = "NameProduct",nullable = false)
private String NameProduct;
Integer getId(){return id;}
public void setId(int id){this.id = id;}
public String getNameProduct(){return NameProduct;}
public void setNameProduct(String NameProduct){this.NameProduct = NameProduct;}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
