Hello Viewers这是一个休眠的ManyToOne RelationShip程序,它共有4个文件(客户,供应商模型类,测试是客户端类,最后一个是休眠配置类)
两个文件都是一个客户,另一个是供应商,都有一个字段和受尊重的表,还提到了外观和答复解决方案,但是该异常显示了附加的异常详细信息[![Exception Details] [1]] [1] < / p>
log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.hibernate.MappingException: Unable to load class [ com/test/model/Customers] declared in Hibernate configuration <mapping/> entry
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2369)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at com.test.client.Test.main(Test.java:18)
Caused by: java.lang.ClassNotFoundException: com/test/model/Customers
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2366)
... 5 more
Customers.java
package com.test.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "Customers100")
public class Customers {
@Id
@Column(name = "custid")
private int customerId;
@Column(name = "custName", length=10)
private String customerName;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="venid",referencedColumnName="vid")
private Vendor parent;
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;
}
public Vendor getParent() {
return parent;
}
public void setParent(Vendor parent) {
this.parent = parent;
}
}
Vendor.java
package com.test.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Vendor100")
public class Vendor{
@Id
@Column(name = "vid")
private int vendorId;
@Column(name = "vname", length=10)
private String vendorName;
public int getVendorId() {
return vendorId;
}
public void setVendorId(int vendorId) {
this.vendorId = vendorId;
}
public String getVendorName() {
return vendorName;
}
public void setVendorName(String vendorName) {
this.vendorName = vendorName;
}
}
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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com/test/model/Customers"></mapping>
<mapping class="com/test/model/Vendor"></mapping>
</session-factory>
</hibernate-configuration>
Test.java
package com.test.client;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import com.test.model.Customers;
import com.test.model.Vendor;
public class Test {
public static void main(String[] args)
{
Configuration cfg = new Configuration();
cfg.configure("com/test/cfg/hibernate.cfg.xml");
SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = factory.openSession();
Vendor v =new Vendor();
v.setVendorId(100);
v.setVendorName("java4s6");
Customers c1=new Customers();
c1.setCustomerId(504);
c1.setCustomerName("customer4");
c1.setParent(v);
Customers c2=new Customers();
c2.setCustomerId(505);
c2.setCustomerName("customer5");
c2.setParent(v);
Customers c3=new Customers();
c3.setCustomerId(506);
c3.setCustomerName("customer6");
c3.setParent(v);
Transaction tx = session.beginTransaction();
//session.save(v);
session.save(c2);
session.save(c2);
session.save(c3);
tx.commit();
session.close();
System.out.println("Many to One with annotation done...!!");
factory.close();
}
}
再次打印出完整的错误详细信息,看看一次
log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.hibernate.MappingException: Unable to load class [ com/test/model/Customers] declared in Hibernate configuration <mapping/> entry
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2369)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at com.test.client.Test.main(Test.java:18)
Caused by: java.lang.ClassNotFoundException: com/test/model/Customers
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2366)
... 5 more