无法创建表 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
Log
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 文件有问题。以上还请各位指教。提前致谢。
I am unable to create a table and insert the values into it using the Hibernate annotations. i am executing using command prompt. Following is my code.
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
Log
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)
I can compile without errors, but not able to run the app. I am guessing there's a problem with jar files. Please advise me on the above as well. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是
一个愚蠢的错误。我的应用程序现在运行完美!
instead of
a silly mistake indeed. My app is now running perfectly!