Entity Manager を使用して永続化しています。
JavaアプリケーションでJPAスタンドアロンを使用すると正常に動作しますが、EJBを実行しようとすると次のようになります
Hibernate: insert into public.student (id, country, student_name) values (null, ?, ?)
id に null を追加しようとしているのはなぜですか?
学生クラス
package com.ejb.entities;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* <p>
* Pojo mapping TABLE public.student
* </p>
*
* <p>
* Generated at Wed May 08 11:27:00 BST 2013
* </p>
*
* @author Salto-db Generator v1.0.16 / EJB3
*
*/
@Entity
@Table(name = "student", schema = "public")
@SuppressWarnings("serial")
public class Student implements Serializable {
/**
* Attribute id.
*/
private Integer id;
/**
* Attribute studentName.
*/
private String studentName;
/**
* Attribute country.
*/
private String country;
/**
* @return id
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
@Basic(optional = false)
public Integer getId() {
return id;
}
/**
* @param id
* new value for id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return studentName
*/
@Basic
@Column(name = "student_name", length = 2147483647)
public String getStudentName() {
return studentName;
}
/**
* @param studentName
* new value for studentName
*/
public void setStudentName(String studentName) {
this.studentName = studentName;
}
/**
* @return country
*/
@Basic
@Column(name = "country", length = 2147483647)
public String getCountry() {
return country;
}
/**
* @param country
* new value for country
*/
public void setCountry(String country) {
this.country = country;
}
}
EJBファサード
package com.ejb.ejbs;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.ejb.entities.Student;
@Stateless
public class UserFacadeEJB implements UserFacadeEJBLocal, UserFacadeEJBRemote {
@PersistenceContext
private EntityManager entityManager;
public List<Student> getAllStudents() {
Query query = entityManager.createQuery("Select st from Student st");
return query.getResultList();
}
public void addStudent(Student student) {
try {
entityManager.persist(student);
} catch (Exception e) {
e.printStackTrace();
}
}
}
EJBテスト
package com.test;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ejb.ejbs.UserFacadeEJBRemote;
import com.ejb.entities.Student;
public class EJBTest {
public static void main(String[] args) {
try {
Properties env = new Properties();
env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
env.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming.client");
env.setProperty("java.naming.provider.url", "jnp://localhost:1099");
env.setProperty(Context.SECURITY_CREDENTIALS, "");
env.setProperty(Context.SECURITY_PRINCIPAL, "");
// env.setProperty(Context.SECURITY_PROTOCOL, "java:/jaas/App");
final String jndiName = "UserFacadeEJB/remote";
Context ic = new InitialContext(env);
Object obj = ic.lookup(jndiName);
UserFacadeEJBRemote foo = (UserFacadeEJBRemote) obj;
Student student = new Student();
student.setCountry("UK");
student.setStudentName("Adeel");
foo.addStudent(student);
} catch (NamingException e) {
e.printStackTrace();
}
System.out.println("--------------all done---------------------------");
}
}
POSTGRES テーブル
CREATE TABLE student
(
id integer NOT NULL DEFAULT nextval('student_sequence'::regclass),
student_name text,
country text,
CONSTRAINT student_pkey PRIMARY KEY (id)
)