JPA学习笔记-EJB-03JPA主键生成策略总结--3

 

package eo;

 

import javax.persistence.AttributeOverride;

import javax.persistence.AttributeOverrides;

import javax.persistence.Column;

import javax.persistence.EmbeddedId;

import javax.persistence.Entity;

import javax.persistence.Table;

 

/**

 * TbStudents entity. @author MyEclipse Persistence Tools

 */

@Entity

@Table(name = "tb_students", catalog = "ejbjpa")

public class TbStudents implements java.io.Serializable {

 

    // Fields

 

    private TbStudentsId id;

    private String info;

 

    // Constructors

 

    /** default constructor */

    public TbStudents() {

    }

 

    /** minimal constructor */

    public TbStudents(TbStudentsId id) {

        this.id = id;

    }

 

    /** full constructor */

    public TbStudents(TbStudentsId id, String info) {

       this.id = id;

       this.info = info;

    }

 

    // Property accessors

    @EmbeddedId

    @AttributeOverrides( {

           @AttributeOverride(name = "name", column = @Column(name = "name", nullable = false)),

           @AttributeOverride(name = "no", column = @Column(name = "no", nullable = false)) })

    public TbStudentsId getId() {

       return this.id;

    }

 

    public void setId(TbStudentsId id) {

       this.id = id;

    }

 

    @Column(name = "info")

    public String getInfo() {

       return this.info;

    }

 

    public void setInfo(String info) {

       this.info = info;

    }

}

TbStudentsId类是主键类,TbStudents是实体类。TbStudentsId是作为TbStudents的一个特殊属性存在的。代码还是以嵌入式的(@Embeddable)方式表现主键的。

下面写个DAO接口

package dao;

 

import javax.ejb.Remote;

 

import eo.TbStudents;

 

@Remote

public interface ITbStudentsService {

   

    public void save (TbStudents tbStudents);

   

}

实现类如下:

package dao.impl;

 

import javax.ejb.Stateless;

import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;

 

import dao.ITbStudentsService;

import eo.TbStudents;

 

@Stateless

public class TbStudentsServiceImpl implements ITbStudentsService {

 

    @PersistenceContext(unitName = "JPAEJBPU")

    private EntityManager entityManager;

 

    @Override

    public void save(TbStudents tbStudents) {

       entityManager.persist(tbStudents);

    }

}

按照前文章节部署到JBOSS上面后,写一个测试代码,代码如下:

    public void test04() {

       try {

 

           Context ctx = getInitialContext();

 

           Object object = ctx.lookup("TbStudentsServiceImpl/remote");

 

           ITbStudentsService tbStudentsService = (ITbStudentsService) PortableRemoteObject

                  .narrow(object, ITbStudentsService.class);

          

           //对主键对象赋值

           TbStudentsId id = new TbStudentsId();

           id.setName("Klose");

           id.setNo("11");

          

//对实体赋值

           TbStudents tbStudents = new TbStudents();

           tbStudents.setId(id);

           tbStudents.setInfo("拜仁");

 

           tbStudentsService.save(tbStudents);

 

       } catch (NamingException e) {

           e.printStackTrace();

       }

    }

第一次执行此方法后:结果数据库如下:
JPA学习笔记-EJB-03JPA主键生成策略总结--3_第1张图片

之后我们不改变任何代码在执行一遍此测试方法,就会发生以下主键重复异常:

Caused by: java.sql.BatchUpdateException: Duplicate entry 'Klose-11' for key 'PRIMARY'

你可能感兴趣的:(DAO,MyEclipse,ejb,jpa,嵌入式)