现在的位置: 首页 > 综合 > 正文

Myeclipse结合mysql和Jboss的EJB实体bean实例

2013年10月10日 ⁄ 综合 ⁄ 共 4728字 ⁄ 字号 评论关闭

 

演示地址:

http://d.download.csdn.net/fd3/aHR0cDovL2RsMi5jc2RuLm5ldC9kb3duMS8yMDA4MDUwMi8wMjE2MzA1MDQwNC5yYXI=!438865

说明:  分两部分,由于第一部分的演示有部分大家看了会不明白的,在第二部分进行了补充。

 

 

import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.SelectCourse;
import com.SelectCourseFacadeRemote;


public class Test {

    
/**
     * @param args
     
*/

    
public static void main(String[] args) {
        Properties props 
= new Properties();
        props.setProperty(
"java.naming.factory.initial""org.jnp.interfaces.NamingContextFactory");
        props.setProperty(
"java.naming.provider.url""localhost:1099");
        props.setProperty(
"java.naming.factory.url.pkgs""org.jboss.naming");

        InitialContext ctx;
        
try {
            ctx 
= new InitialContext(props);
            SelectCourseFacadeRemote remote 
= (SelectCourseFacadeRemote) ctx.lookup("SelectCourseFacade/remote");
            SelectCourse sc 
= new SelectCourse();
            sc.setId((
long)500);
            sc.setCourseId(
52);
            sc.setStuId(
533);
            
            remote.save(sc);
            ctx.close();
            
        }
 catch (NamingException e) {
            System.
out.println(e.getMessage());
        }
 finally {

        }

    }


}

 

persistence.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="myEjbEntityBean" transaction-type="JTA">
          
<jta-data-source>java:/DefaultMySqlDS</jta-data-source>
    
</persistence-unit>
  
</persistence>

 

 

package com;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * SelectCourse entity.
 * 
 * @author MyEclipse Persistence Tools
 
*/

@Entity
@Table(name 
= "select_course", catalog = "test", uniqueConstraints = {})
public class SelectCourse implements java.io.Serializable {

    
// Fields

    
private Long id;
    
private Integer stuId;
    
private Integer courseId;

    
// Constructors

    
/** default constructor */
    
public SelectCourse() {
    }


    
/** minimal constructor */
    
public SelectCourse(Long id) {
        
this.id = id;
    }


    
/** full constructor */
    
public SelectCourse(Long id, Integer stuId, Integer courseId) {
        
this.id = id;
        
this.stuId = stuId;
        
this.courseId = courseId;
    }


    
// Property accessors
    @Id
    @Column(name 
= "ID", unique = true, nullable = false, insertable = true, updatable = true)
    
public Long getId() {
        
return this.id;
    }


    
public void setId(Long id) {
        
this.id = id;
    }


    @Column(name 
= "stuId", unique = false, nullable = true, insertable = true, updatable = true)
    
public Integer getStuId() {
        
return this.stuId;
    }


    
public void setStuId(Integer stuId) {
        
this.stuId = stuId;
    }


    @Column(name 
= "courseId", unique = false, nullable = true, insertable = true, updatable = true)
    
public Integer getCourseId() {
        
return this.courseId;
    }


    
public void setCourseId(Integer courseId) {
        
this.courseId = courseId;
    }


}

 

package com;

import java.util.List;
import javax.ejb.Remote;

/**
 * Remote interface for SelectCourseFacade.
 * 
 * @author MyEclipse Persistence Tools
 
*/

@Remote
public interface SelectCourseFacadeRemote {
    
/**
     * Perform an initial save of a previously unsaved SelectCourse entity. All
     * subsequent persist actions of this entity should use the #update()
     * method.
     * 
     * @param entity
     *            SelectCourse entity to persist
     * @throws RuntimeException
     *             when the operation fails
     
*/

    
public void save(SelectCourse entity);

    
/**
     * Delete a persistent SelectCourse entity.
     * 
     * @param entity
     *            SelectCourse entity to delete
     * @throws RuntimeException
     *             when the operation fails
     
*/

    
public void delete(SelectCourse entity);

    
/**
     * Persist a previously saved SelectCourse entity and return it or a copy of
     * it to the sender. A copy of the SelectCourse entity parameter is returned
     * when the JPA persistence mechanism has not previously been tracking the
     * updated entity.
     * 
     * @param entity
     *            SelectCourse entity to update
     * @returns SelectCourse the persisted SelectCourse entity instance, may not
     *          be the same
     * @throws RuntimeException
     *             if the operation fails
     
*/

    
public SelectCourse update(SelectCourse entity);

    
public

抱歉!评论已关闭.