我正在尝试将Java对象持久保存到Mysql数据库。但是我面临以下错误。
ClientDemo.java
package com.infybank;
import java.util.Scanner;
import org.hibernate.HibernateException;
public class ClientDemo {
public static void main(String args[])
{
CustomerDAO custdao=new CustomerDAO();
try
{
System.out.println("CREATE");
System.out.println("Enter the customer details");
Scanner sc=new Scanner(System.in);
System.out.println("Enter the customer number");
int d=sc.nextInt();
System.out.println("Enter the customer name");
String name=sc.next();
Customer cust=new Customer(d,name);
custdao.addCustomer(cust);
System.out.println("One recort inserted");
}
catch(HibernateException ex)
{
System.out.println("Exception "+ex);
}
}}`
HibernateUtil.java 包com.infybank;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static
{
try
{
Configuration configuration =new Configuration().configure("/config/hibernate.cfg.xml");
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory=configuration.buildSessionFactory(serviceRegistry);
}
catch(Throwable ex)
{
System.err.println("Initial sessionFactory creation failed"+ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}}
这是hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">Anak@1604</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="com.infybank.Customer"/>
</session-factory>
</hibernate-configuration>
Customer.java
package com.infybank;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="customer")
public class Customer {
@Id
@Column(name="CUSTOMERID")
private int customerId;
@Column(name="CUSTOMERNAME")
private String customerName;
public Customer(int id,String name)
{
this.customerId=id;
this.customerName=name;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
CustomerDAO.java
package com.infybank;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class CustomerDAO implements ICustomer{
@Override
public void addCustomer(Customer cust) {
// TODO Auto-generated method stub
Session session=HibernateUtil.getSessionFactory().openSession();
Transaction tx=session.beginTransaction();
session.save(cust);
tx.commit();
session.close();
}
}
错误 异常org.hibernate.MappingException:未知实体:com.infybank.Customer
在尝试运行代码时遇到上述错误
答案 0 :(得分:0)
至少Hibernate实体不应具有带有参数的此类构造函数:
{
name: 'backend_field_created_at',
alias: 'createdAt',
render: (row) => { return (<span>{`${date(row.createdAt)}`}</span>) },
sortable: true,
type: 'text'
}
有关更多信息,请参见this answer。
答案 1 :(得分:0)
默认情况下,所有实体类都应具有默认构造函数。 Hibernate要求这样做,因为它们是通过反射实例化的。
我相信您只需要向您的Customer类添加一个空的构造函数,就可以了
public Customer(){}