“不能从静态上下文引用非静态方法". JPA Java [英] "non static method cannot be referenced from a static context" JPA Java
问题描述
我从此行收到无法从静态上下文引用非静态方法"错误:
I'm getting the "non static method cannot be referenced from a static context" error from this line:
createStudent("stu00001", new Date(631152000000)), "m", "WB", new Type_Name("Bob", "", "Smith"));
您如何正确形成日期"?我看过API并尝试了不同的方法,但仍然收到日期错误.
How do you form 'Date' properly? I've had a look on the API's and tried different things but I still get an error for date.
package grade_db;
import bean.Student;
import bean.Type_Name;
import bean.University;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
*
* @author Sam
*/
public class Main {
EntityManager em;
EntityManagerFactory emf;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
EntityManagerFactory emf = Persistence.createEntityManagerFactory("db/grades.odb");
EntityManager em;
em = emf.createEntityManager();
createStudent("stu00001", new Date(631152000000)), "m", "WB", new Type_Name("Bob", "", "Smith"));
em.close();
emf.close();
}
public Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name){
Student stu = new Student();
stu.setDob(dob);
stu.setGender(gender);
stu.setName(name);
stu.setNationality(nationality);
stu.setCampus_id("cam00001");
stu.setCourse_id(null);
stu.setStudent_id(student_id);
em.persist(stu);
return stu;
}
}
推荐答案
问题是您正在尝试从main()
中的静态上下文调用实例方法createStudent()
.如果将您的createStudent()
方法更改为静态方法,则应该可以进行以下操作:
The problem is that you're trying to call the instance method createStudent()
from a static context in main()
. If you change your createStudent()
method to be static, you should be good to go:
public static Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name) {
// ... And so on
}
编辑:OP指出,访问变量em
和emf
时,仅此更改就给他带来了另一个错误.要解决此问题,您还需要将这些变量设置为static
:
EDIT: OP pointed out that this change alone gives him another error when accessing the variables em
and emf
. To fix that, you'd need to make those variables static
, too:
static EntityManager em;
static EntityManagerFactory emf;
那时,您班级中的所有内容都是静态的.假设这是一个简单的单例或示例(由于该类称为Main
,我很容易假设),则使所有内容静态化就可以了.总的来说,代码看起来像这样:
At that point, everything in your class is static. Assuming this is a simple one-off or example -- which I'm comfortable assuming since the class is called Main
-- making everything static is just fine. In all, the code would look like this:
package grade_db;
import bean.Student;
import bean.Type_Name;
import bean.University;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
*
* @author Sam
*/
public class Main {
static EntityManager em;
static EntityManagerFactory emf;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
EntityManagerFactory emf = Persistence.createEntityManagerFactory("db/grades.odb");
EntityManager em;
em = emf.createEntityManager();
createStudent("stu00001", new Date(631152000000)), "m", "WB", new Type_Name("Bob", "", "Smith"));
em.close();
emf.close();
}
public static Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name){
Student stu = new Student();
stu.setDob(dob);
stu.setGender(gender);
stu.setName(name);
stu.setNationality(nationality);
stu.setCampus_id("cam00001");
stu.setCourse_id(null);
stu.setStudent_id(student_id);
em.persist(stu);
return stu;
}
}
这篇关于“不能从静态上下文引用非静态方法". JPA Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!