java引用非静态字串,“不能从静态上下文引用非静态方法”。 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"));

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;

}

}

解决方案

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

}

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;

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值