Hibernate在使用EJB时​​尝试向ID添加null

时间:2013-05-08 10:51:50

标签: hibernate jpa null ejb persistence

我正在使用实体管理器来保持。

如果我在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;
    }

}

EJBFacade

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();
        }

    }

}

EJBTest

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 TABLE

CREATE TABLE student
(
  id integer NOT NULL DEFAULT nextval('student_sequence'::regclass),
  student_name text,
  country text,
  CONSTRAINT student_pkey PRIMARY KEY (id)
)

3 个答案:

答案 0 :(得分:0)

可能问题与GenerationType.AUTO有关,请尝试使用GenerationType.TABLE,您可以找到更多信息here

答案 1 :(得分:0)

您需要按照如下所示的方式使用它

 @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq-gen")
    @SequenceGenerator(name="seq-gen", sequenceName="student_sequence", initialValue=25, allocationSize=12)
    @Basic(optional = false)
    public Integer getId() {
        return id;
    }

答案 2 :(得分:0)

问题的答案是我自己的错误,因为我没有在数据库中正确创建表。