tanyongbing 发表于 2013-2-3 13:37:11

在JBoss下发布EJB3.0实现增删改查

persistence.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns:persistence="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd ">

<persistence-unit name="genlot" transaction-type="JTA">
<jta-data-source>java:GenlotDS</jta-data-source>
<properties>
   <property name="hibernate.show_sql" value="true"></property>
   <property name="hibernate.format_sql" value="true"></property>
</properties>
</persistence-unit>
</persistence>
jndi.properties文件
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jnp.interfaces

实体Bean

package com.genlot.loms.ejb3.entity;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "ss_user")
public class User implements java.io.Serializable{

private static final long serialVersionUID = -8692000975878306489L;

private Integer id;

private String name;
private String password;
private String sex;
private Date birthday;
private String address;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, length = 20)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}

@Column(name = "name", nullable = false, length = 19)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "password", nullable = false, length = 19)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "sex", nullable = false, length = 19)
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Column(name="birthday" )
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Column(name = "address", nullable = false, length = 19)
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

package com.genlot.loms.ejb3.bussiness;

import java.util.List;

import com.genlot.loms.ejb3.entity.User;

public interface UserBussiness {
public void save (User user);

public void update (User user);

public void delete (Integer userId);

public User getUser(Integer userId);

public List<User> findUserAll();
}


package com.genlot.loms.ejb3.bussiness.impl;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.genlot.loms.ejb3.bussiness.UserBussiness;
import com.genlot.loms.ejb3.entity.User;

@Stateless
@Remote(UserBussiness.class)
public class UserBussinessImpl implements UserBussiness {

@PersistenceContext(unitName="genlot") EntityManager em;

public void save(User user) {
em.persist(user);
}

public void update(User user) {
em.merge(user);
}

public void delete(Integer userId) {
em.remove(em.getReference(User.class, userId));
}

public User getUser(Integer userId) {
return em.find(User.class, userId);
}

@SuppressWarnings("unchecked")
public List<User> findUserAll() {
return em.createQuery(" from User where 1 = 1").getResultList();
}

}
页: [1]
查看完整版本: 在JBoss下发布EJB3.0实现增删改查