我是hibernate的新手,我的代码是关于使用注释的每个层次结构的hibernate表。我得到以下错误

时间:2014-12-28 11:21:56

标签: hibernate

0 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.4.0.GA
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
109 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.1.0.GA
1920 [main] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find             org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not     enabled.
2077 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class:   com.rtr.annotation.Employee2Ann
2139 [main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity com.rtr.annotation.Employee2Ann on table empAnn10
2202 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: com.rtr.annotation.Regular_Employee2Ann
2217 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: com.rtr.annotation.Contract_Employee2Ann
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection
    at        org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at com.rtrsi.annotation.StoreData4Ann.main(StoreData4Ann.java:19)
Caused by: java.sql.SQLException: Access denied for user ''@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:911)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3953)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1276)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2048)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
... 5 more

我的cfg.xml是

<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hbm2ddl.auto">create</property>
<property name="connection.url">jdbc:mysql://localhost:3306/rea</property>
<property name="connectiuon.username">root</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.show_sql">true</property>
<mapping class="com.rtri.annotation.Employee2Ann"/>
<mapping class="com.rtri.annotation.Regular_Employee2Ann"/>
<mapping class="com.rtri.annotation.Contract_Employee2Ann"/>
</session-factory>
</hibernate-configuration>

主要课程是

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class StoreData4Ann {
    public static void main(String[] args) {
        //1.
        Configuration config=new AnnotationConfiguration();
        config.configure("employee2Ann.cfg.xml");
        //2.
        SessionFactory factory=config.buildSessionFactory();
        //3
        Session session=factory.openSession();
        //4.
        Transaction t=session.beginTransaction();

        Employee2Ann e2=new Employee2Ann();
        e2.setName("rahul");

        Regular_Employee2Ann r2=new Regular_Employee2Ann();
        r2.setSalary(50000);
        r2.setName("arun");

        Contract_Employee2Ann c2=new Contract_Employee2Ann();
        c2.setName("ravi");
        c2.setContract_duration("5 Months");
        c2.setPay_per_hour(150);

        session.saveOrUpdate(e2);
        session.saveOrUpdate(r2);
        session.saveOrUpdate(c2);

        t.commit();
        session.close();
        System.out.println("succefully saved");

    }
}

和我坚持不懈的课程

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table(name="empAnn10")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="classname", discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value ="emp2ann")
public class Employee2Ann {
    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    @Column(name="name")
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

另一个持久的

@Entity
@DiscriminatorValue(value="regemp2ann")
public class Regular_Employee2Ann extends Employee2Ann {
    @Column(name="bonus")

    private int bonus;
    @Column(name="salary")
    private float salary;
    public int getBonus() {
    return bonus;
    }
    public void setBonus(int bonus) {
    this.bonus = bonus;
    }
    public float getSalary() {
    return salary;
    }
    public void setSalary(float salary) {
    this.salary = salary;
    } 
}

另一个持久的课程

@Entity
@DiscriminatorValue(value="contemp2ann")
public class Contract_Employee2Ann extends Employee2Ann{

    @Column(name="pay_per_hour")
    private float pay_per_hour;
    @Column(name="contract_duration")
    private String contract_duration;
    public float getPay_per_hour() {
        return pay_per_hour;
    }
    public void setPay_per_hour(float pay_per_hour) {
        this.pay_per_hour = pay_per_hour;
    }
    public String getContract_duration() {
        return contract_duration;
    }
    public void setContract_duration(String contract_duration) {
        this.contract_duration = contract_duration;
    }
}

我使用过hibernate验证器,常用注释,注释,hibernate3,log4j,asm,dom4j,cglib,常用日志记录,常见收集,ejb3 persistance,slf4j,mysql连接器jar

1 个答案:

答案 0 :(得分:0)

配置中的Type-O

<property name="connectiuon.username">root</property>

更改为

<property name="connection.username">root</property>

再试一次