2011-12-19 87 views
1

我無法創建表並使用Hibernate註釋將值插入到表中。我正在執行使用命令提示符。以下是我的代碼。無法創建表Hibernate註釋

Contact.java

import javax.persistence.*; 

@Entity 
@Table(name="Contact") 
public class Contact { 

private String firstName; 
private String lastName; 
private String email; 
private long id; 

@Column(name="Email") 
public String getEmail() { 
    return email; 
} 

@Column(name="First_Name") 
public String getFirstName() { 
    return firstName; 
} 

@Column(name="Last_Name") 
public String getLastName() { 
    return lastName; 
} 

public void setEmail(String s) { 
email = s; 
} 

public void setFirstName(String s) { 
firstName = s; 
} 

public void setLastName(String s) { 
lastName = s; 
} 

@Id 
@GeneratedValue 
@Column(name="id") 
public long getId() { 
return id; 
} 

public void setId(long l) { 
id = l; 
} 
} 

FirstExample.java

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

public class FirstExample { 
public static void main(String[] args) { 
    Session session = null; 
    try{ 
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
    session =sessionFactory.openSession(); 
    System.out.println("Inserting Record"); 
    Contact contact = new Contact(); 
    contact.setId(3); 
    contact.setFirstName("Tausif"); 
    contact.setLastName("rahmathullah"); 
    contact.setEmail("[email protected]"); 
    session.save(contact); 
    session.update(contact); 
    System.out.println("updated"); 
    }catch(Exception e){ 
    System.out.println(e.getMessage()); 
    }finally{ 
    if(session!=null){ 

    session.flush(); 
    session.close(); 
    } 
    } 
    } 
} 

的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 
"http://hibernate.sourceforge.net/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/my_db</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password">root</property> 
    <property name="hibernate.connection.pool_size">10</property> 
    <property name="show_sql">true</property> 
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 
    <property name="hibernate.hbm2ddl.auto">update</property> 
    <mapping resource="Contact"/> 
</session-factory> 
</hibernate-configuration> 

個jarfiles中

antlr-2.7.6.jar 
commons-collections-3.1.jar 
dom4j-1.6.1.jar 
hibernate3.jar 
hibernate3.3.1-slf4j-api-1.5.2.jar 
hibernate-jpa-2.0-api-1.0.1.Final.jar 
javassist-3.4.ga.jar 
jcl-over-slf4j-1.5.2.jar 
jta-1.1.jar 
log4j.jar 
mysql-connector-java-5.0.0-bin.jar 
slf4j-log4j-1.5.2.jar 
ejb3-persistence.jar 
hibernate-annotation.jar 
hibernate-commons-annotations.jar 

登錄

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version). 
log4j:WARN Please initialize the log4j system properly. 
Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE 
    at org.hibernate.type.BasicTypeRegistry.<init>(BasicTypeRegistry.java:94) 
    at org.hibernate.type.TypeResolver.<init>(TypeResolver.java:59) 
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:250) 
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:302) 
    at org.hibernate.cfg.AnnotationConfiguration.<init>(AnnotationConfiguration.java:87) 
    at FirstExample.main(FirstExample.java:15) 

我可以編譯沒有錯誤,但無法運行應用程序。我猜測jar文件有問題。請告知我以上。提前致謝。

+0

也變得 資源:聯繫未找到..請糾正這一點以及 – javaDisciple

回答

2

代替

<mapping resources> use <mapping class> 

一個愚蠢的錯誤確實如此。我的應用程序現在運行完美!