INFO org.hibernate.impl.SessionFactoryObjectFactory - 未将工厂绑定到JNDI,未配置JNDI名称

时间:2015-05-09 06:29:19

标签: java hibernate

当我运行代码时,我收到以下错误:

  

[main] INFO org.hibernate.impl.SessionFactoryObjectFactory - 不是   将工厂绑定到JNDI,没有配置JNDI名称..

App.java

package com.techvision.main;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.techvision.model.Organization;
import com.techvision.model.Person;
import com.techvision.persistence.HibernateUtil;

public class App {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
          Session session = HibernateUtil.getSessionFactory().openSession();

          Transaction transaction = null;

          try {
            transaction = session.beginTransaction();

            //parent object
              Organization org= new Organization();
              org.setOrgname("ABCD");
              org.setOrgcode("ABCD01");
              org.setOrgstatus("A".charAt(0));

              Person person1=new Person();
              person1.setFirstname("firstname1");
              person1.setLastname("lastname1");
              person1.setPersonstatus("A".charAt(0));

              org.getPersons().add(person1);

              Person person2=new Person();
              person2.setFirstname("firstname2");
              person2.setLastname("lastname2");
              person2.setPersonstatus("A".charAt(0));

              org.getPersons().add(person2);
              session.save(org);

              transaction.commit();
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Organization.java     包com.techvision.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="organization")
public class Organization {

@Id
@GeneratedValue
private int orgid; 
private String orgname; 
private String orgcode; 
private char orgstatus;


@OneToMany(mappedBy="organization", cascade=CascadeType.ALL)
    private Set<Person> persons;


public Set<Person> getPersons() {
    return persons;
}
public void setPersons(Set<Person> persons) {
    this.persons = persons;
}

public void addPerson(Person person) {
      this.persons.add(person);
      person.setOrganization(this);
    }

public int getOrgid() {
    return orgid;
}
public void setOrgid(int orgid) {
    this.orgid = orgid;
}
public String getOrgname() {
    return orgname;
}
public void setOrgname(String orgname) {
    this.orgname = orgname;
}
public String getOrgcode() {
    return orgcode;
}
public void setOrgcode(String orgcode) {
    this.orgcode = orgcode;
}
public char getOrgstatus() {
    return orgstatus;
}
public void setOrgstatus(char orgstatus) {
    this.orgstatus = orgstatus;
}

}

Person.java

package com.techvision.model;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="person")
public class Person {

@Id
@GeneratedValue
private int personid; 

private String firstname; 
private String lastname; 
private char personstatus;

@ManyToOne
@JoinColumn(name = "orgid")
private Organization organization;

public Organization getOrganization() {
    return organization;
}
public void setOrganization(Organization organization) {
    this.organization = organization;
}
public int getPersonid() {
    return personid;
}
public void setPersonid(int personid) {
    this.personid = personid;
}

public String getFirstname() {
    return firstname;
}
public void setFirstname(String firstname) {
    this.firstname = firstname;
}
public String getLastname() {
    return lastname;
}
public void setLastname(String lastname) {
    this.lastname = lastname;
}
public char getPersonstatus() {
    return personstatus;
}
public void setPersonstatus(char personstatus) {
    this.personstatus = personstatus;
}


}

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="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest1</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <mapping class="com.techvision.model.Organization"></mapping>
    <mapping class="com.techvision.model.Person"></mapping>
    </session-factory>
</hibernate-configuration>

HibernateUtil.java

package com.techvision.persistence;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
     private static final SessionFactory sessionFactory = buildSessionFactory();

        private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure().buildSessionFactory();

        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        }

        public static SessionFactory getSessionFactory() {
        return sessionFactory;
        }

        public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
        }

}

0 个答案:

没有答案