伙计们我是hibernate的新手..我正在尝试使用hibernate在java中运行插入函数但是它给了我异常..任何帮助..在此先感谢
Exception in thread "main" java.lang.NoSuchFieldError: namingStrategy
at org.hibernate.cfg.AnnotationConfiguration.reset(AnnotationConfiguration.java:250)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:125)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:119)
at org.hibernate.cfg.AnnotationConfiguration.<init>(AnnotationConfiguration.java:108)
at InsertData.main(InsertData.java:11)
&#13;
import javax.persistence.*;
@Entity
@Table(name="dataprovider")
public class DataProvider {
@Id @GeneratedValue
@Column(name="id")
private int user_id;
@Column(name="name")
private String user_name;
@Column(name="description")
private String user_desc;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_desc() {
return user_desc;
}
public void setUser_desc(String user_desc) {
this.user_desc = user_desc;
}
}
&#13;
InsertData.java
import javax.persistence.*;
@Entity
@Table(name="dataprovider")
public class DataProvider {
@Id @GeneratedValue
@Column(name="id")
private int user_id;
@Column(name="name")
private String user_name;
@Column(name="description")
private String user_desc;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_desc() {
return user_desc;
}
public void setUser_desc(String user_desc) {
this.user_desc = user_desc;
}
}
&#13;
的hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration>
<session-factory>
<!-- Related to the connection START -->
<property name="connection.driver_class">com.mysql.jdbc.Driver </property>
<property name="connection.url">jdbc:mysql://localhost:3306/mydb </property>
<property name="connection.user">root </property>
<property name="connection.password">root</property>
<!-- Related to the connection END -->
<!-- Related to hibernate properties START -->
<property name="show_sql">true</property>
<property name="dialet">org.hibernate.dialet.MYSQLDialet</property>
<property name="hbm2ddl.auto">create</property>
<!-- Related to hibernate properties END-->
<!-- Related to mapping START-->
<mapping resource="user.hbm.xml" />
<!-- Related to the mapping END -->
</session-factory>
</hibernate-configuration>
&#13;
答案 0 :(得分:0)
对于注释,您需要将user.hbm.xml
更改为packagename.classname
<!-- Related to mapping START-->
<mapping resource="user.hbm.xml" />
<!-- Related to the mapping END -->
您必须在该案例中使用您的包名。
<mapping class="yourPackageName.DataProvider" />
其中,
yourPackageName = com.something。
DataProvider =您的班级名称
相关链接:http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example-annotation/