复合主键的映射:在Hibernate中是通过一个主键类来完成复合主键的映射,主键类复写了equals和hashCode方法,用来完成比较功能,以便判断主键值是否重复。
EJB3中也使用该方式完成映射,只是改为通过Annotation来配置关系。
方法见四、EJB实体Bean开发
并且保证JBoss中有该数据库连接jar包,例如:
注意在这里配置数据源连接和选择要连接的数据库。
Driver处选择的数据源同样是在MyEclipse中配置的,方法同Hibernate之前的准备工作相同
配置完成后会在EJB端项目中创建一个persitence.xml文件,在该文件中进行连接等配置
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="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 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="mysql" transaction-type="JTA"> <jta-data-source>java:jdbc/mysql</jta-data-source> </persistence-unit>
</persistence> |
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="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 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="mysql" transaction-type="JTA"> <jta-data-source>java:jdbc/mysql</jta-data-source> <properties> <property name="hibernate.show_sql" value="true"/> </properties> </persistence-unit>
</persistence> |
映射生成了两个实体Bean
一个主键类、一个实体类
package org.mldn.lin.entity;
import javax.persistence.Column; import javax.persistence.Embeddable;
//@Embeddable表示该类是一个复合主键类,该类同样覆写了equals和hashcode方法 @Embeddable public class UserCourseId implements java.io.Serializable {
private String userid;
private Integer cid;
public UserCourseId() { }
public UserCourseId(String userid, Integer cid) { this.userid = userid; this.cid = cid; }
@Column(name = "userid", unique = false, nullable = false, insertable = true, updatable = true, length = 20) public String getUserid() { return this.userid; }
public void setUserid(String userid) { this.userid = userid; }
@Column(name = "cid", unique = false, nullable = false, insertable = true, updatable = true) public Integer getCid() { return this.cid; }
public void setCid(Integer cid) { this.cid = cid; }
public boolean equals(Object other) { if ((this == other)) return true; if ((other == null)) return false; if (!(other instanceof UserCourseId)) return false; UserCourseId castOther = (UserCourseId) other;
return ((this.getUserid() == castOther.getUserid()) || (this .getUserid() != null && castOther.getUserid() != null && this.getUserid().equals( castOther.getUserid()))) && ((this.getCid() == castOther.getCid()) || (this.getCid() != null && castOther.getCid() != null && this.getCid().equals( castOther.getCid()))); }
public int hashCode() { int result = 17;
result = 37 * result + (getUserid() == null ? 0 : this.getUserid().hashCode()); result = 37 * result + (getCid() == null ? 0 : this.getCid().hashCode()); return result; }
} |
package org.mldn.lin.entity;
import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.Table;
@Entity @Table(name = "user_course", catalog = "testdb", uniqueConstraints = {}) public class UserCourse implements java.io.Serializable {
private UserCourseId id;
private Integer status;
public UserCourse() { }
public UserCourse(UserCourseId id) { this.id = id; }
public UserCourse(UserCourseId id, Integer status) { this.id = id; this.status = status; }
// Property accessors //表示使用复合主键 @EmbeddedId // 复合主键中包含以下两个属性 @AttributeOverrides( { @AttributeOverride(name = "userid", column = @Column(name = "userid", unique = false, nullable = false, insertable = true, updatable = true, length = 20)), @AttributeOverride(name = "cid", column = @Column(name = "cid", unique = false, nullable = false, insertable = true, updatable = true)) }) public UserCourseId getId() { return this.id; }
public void setId(UserCourseId id) { this.id = id; }
@Column(name = "status", unique = false, nullable = true, insertable = true, updatable = true) public Integer getStatus() { return this.status; }
public void setStatus(Integer status) { this.status = status; }
} |
srcànewàEJB3 SessionBean
package org.mldn.lin.session;
import java.util.List;
import javax.ejb.Remote;
import org.mldn.lin.entity.UserCourseId;
@Remote public interface UserCourseDAORemote { public boolean doCreate(UserCourseDAO uc) throws Exception;
public UserCourseDAO findById(UserCourseId id) throws Exception;
public List<UserCourseDAO> findByCid(int cid) throws Exception;
}
|
package org.mldn.lin.session;
import java.util.List;
import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query;
import org.mldn.lin.entity.UserCourseId;
@Stateless public class UserCourseDAO implements UserCourseDAORemote {
@PersistenceContext private EntityManager em;
public boolean doCreate(UserCourseDAO uc) throws Exception { // TODO Auto-generated method stub this.em.persist(uc); return true; }
public List<UserCourseDAO> findByCid(int cid) throws Exception { // TODO Auto-generated method stub String jpql="FROM UserCourse WHERE id.cid=?"; Query q=this.em.createQuery(jpql); q.setParameter(1, cid); return q.getResultList(); }
public UserCourseDAO findById(UserCourseId id) throws Exception { // TODO Auto-generated method stub UserCourseDAO uc=this.em.find(UserCourseDAO.class, id); return uc; }
}
|
发布成功
通过测试类进行测试,需要将 拷贝到lib下即可
package org.mldn.lin.test;
import java.util.Properties;
import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException;
import org.mldn.lin.entity.UserCourse; import org.mldn.lin.entity.UserCourseId; import org.mldn.lin.session.UserCourseDAORemote;
public class Test {
public static void main(String[] args) { // 定义查找参数,通过定义这些参数找到该公共空间 Properties pro = new Properties(); pro.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); pro.setProperty("java.naming.provider.url", "192.168.1.187:1099"); pro.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming"); // 通过JNDI查找发布的接口 try { Context ctx = new InitialContext(pro);
// 开始查找接口 UserCourseDAORemote dao = (UserCourseDAORemote) ctx.lookup("UserCourseDAO/remote");
UserCourse uc = new UserCourse(); uc.setId(new UserCourseId()); uc.getId().setCid(1); uc.getId().setUserid("mldn"); uc.setStatus(1);
// 打印结果 //System.out.println(dao.doCreate(uc)); //System.out.println(dao.findById(uc.getId())); System.out.println(dao.findByCid(1).size());
} catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |