this Error shown to me.
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
此错误向我显示。 线程" main"中的例外情况org.hibernate.service.spi.ServiceException:无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 在org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244) 在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189) 在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51) 在org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) 在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189) 在org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352) 在org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111) 在org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83) 在org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418) 在org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724) at conn.ConnectionUtil.getSession(ConnectionUtil.java:26) 在dao.ApplicationDAO.AddCourse(ApplicationDAO.java:16) 在controller.TestMain.main(TestMain.java:21) 引起:org.hibernate.boot.registry.selector.spi.StrategySelectionException:无法将名称[org.hibernate.dialect.MYSQLDialect]解析为策略[org.hibernate.dialect.Dialect] at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:113) at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:162) at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:126) 在org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:120) 在org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:74) 在org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:51) 在org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) 在org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234) ......还有16个
MY POJO CLASSES ARE
package entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="STUDENT")
public class Student {
private int sid;
private String sname;
private long phone;
private Course course;
@Id
@GenericGenerator(name="incr", strategy="org.hibernate.id.IncrementGenerator")
@GeneratedValue(generator="incr")
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
Second one is
package entity;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="COURSE")
public class Course {
private int cid;
private String cname;
private int fees;
private List<Student>student;
@Id
@GenericGenerator(name ="incr" ,strategy="org.hibernate.Id.IncrementGenerator")
@GeneratedValue
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getFees() {
return fees;
}
public void setFees(int fees) {
this.fees = fees;
}
public List<Student> getStudent() {
return student;
}
public void setStudent(List<Student> student) {
this.student = student;
}
}
this is DAO class
package dao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import conn.ConnectionUtil;
import entity.Course;
import entity.Student;
public class ApplicationDAO {
private Session ses;
private Transaction ts;
private Course c;
public void AddCourse(Course c){
ConnectionUtil.getSession();
ts = ses.beginTransaction();
ses.save(c);
ts.commit();
ses.close();
}
public void AddStudent(Student s){
ConnectionUtil.getSession();
ts= ses.beginTransaction();
ses.save(s);
ts.commit();
ses.close();
}
}
This is main class
package controller;
import java.util.List;
import dao.ApplicationDAO;
import entity.Course;
import entity.Student;
public class TestMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
Course c;
Student s1,s2,s3;
List<Student>ls;
ApplicationDAO dao = new ApplicationDAO();
c = new Course();
c.setCname("Java");
c.setFees(6000);
dao.AddCourse(c);
s1 = new Student();
s1.setSname("Prity");
s1.setPhone(977869543);
s1.setCourse(c);
dao.AddStudent(s1);
s2 = new Student();
s2.setSname("Rakul");
s2.setPhone(546856);
s2.setCourse(c);
dao.AddStudent(s2);
s3 = new Student();
s3.setSname("Ajay");
s3.setPhone(245687);
s3.setCourse(c);
dao.AddStudent(s3);
System.out.println("Done");
}
}
this is connection class
package conn;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import entity.Course;
import entity.Student;
public class ConnectionUtil {
public static Session getSession(){
Properties p = new Properties();
p.setProperty("hibernate.dialect", "org.hibernate.dialect.MYSQLDialect");
p.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
p.setProperty("hibernate.connection.url","jdbc:mysql://localhost:3306/nilamani" );
p.setProperty("hibernate.connection.username", "root");
p.setProperty("hibernate.connection.password", "");
p.setProperty("hibernate.hbm2ddl.auto", "update");
p.setProperty("hibernate.show_sql", "true");
p.setProperty("hibernate.format_sql", "true");
Configuration cofig = new Configuration();
cofig.addPackage("entities").addProperties(p).addAnnotatedClass(Course.class).addAnnotatedClass(Student.class);
SessionFactory fac = cofig.buildSessionFactory();
Session ses = fac.openSession();
return ses;
}
}`
答案 0 :(得分:0)
方言的名称区分大小写。而不是
org.hibernate.dialect.MYSQLDialect
应该使用(小写&#39; y&#39;):
org.hibernate.dialect.MySQLDialect
作为旁注,作为documented,此方言适用于MySQL之前的版本5.对于较新版本,有更好的选项,例如:MySQL5Dialect,MySQL5InnoDBDialect和{{3 }}