⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usersession.java

📁 struts+ejb开发简单借书系统,用的是mysql数据库.
💻 JAVA
字号:
package de.laliluna.tutorial.library.session.ejb;

import java.rmi.RemoteException;
import java.util.Collection;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import de.laliluna.tutorial.library.entity.interfaces.UserLocal;
import de.laliluna.tutorial.library.entity.interfaces.UserLocalHome;
import de.laliluna.tutorial.library.entity.interfaces.UserValue;

/**
 * XDoclet-based session bean.  The class must be declared
 * public according to the EJB specification.
 *
 * To generate the EJB related files to this EJB:
 *		- Add Standard EJB module to XDoclet project properties
 *		- Customize XDoclet configuration for your appserver
 *		- Run XDoclet
 *
 * Below are the xdoclet-related tags needed for this EJB.
 * 
 * @ejb.bean name="UserSession"
 *           display-name="UserSession"
 *           description="Description for UserSession"
 *           jndi-name="ejb/UserSessionHome"
 *           type="Stateless"
 *           view-type="remote"
 */
public class UserSession implements SessionBean {

	/**
	 * This method return all users as a collection
	 * 
	 * Diese Methode liest alle Benutzer aus und 
	 * gibt diese als Collection zur點k
	 * 
	 * @ejb.interface-method view-type = "both"
	 */
	public Collection getAllUsers() throws EJBException {
		try {
			InitialContext context = new InitialContext();
			
			//Get the home interface with JNDI from the application server
			//Home Interface 黚er JNDI beim Appliacation Server holen
			UserLocalHome userLocalHome = (UserLocalHome)context.lookup(UserLocalHome.JNDI_NAME);
			
			//find all users and return them as collection
			//suche alle Benutzer und gibt diese als Collection zur點k
			return userLocalHome.findAll();
			
		} catch (NamingException e) {
			throw new EJBException(e.getMessage());
		} catch (FinderException e) {
			throw new EJBException(e.getMessage());
		}
	}
	
	/**
	 * This methode get a user by primary key
	 * 
	 * Diese Methode liest einen Benutzer anhand seines Prim鋜schl黶sels aus
	 * 
	 * @ejb.interface-method view-type = "both"
	 */
	public UserValue getUserByPrimaryKey(Integer primaryKey) throws EJBException {
		try {
			InitialContext context = new InitialContext();
			
			//Get the home interface with JNDI from the application server
			//Home Interface 黚er JNDI beim Appliacation Server holen
			UserLocalHome userLocalHome = (UserLocalHome)context.lookup(UserLocalHome.JNDI_NAME);
			
			//find a user by primary key
			//suche ein Buch anhand seines Prim鋜schl黶sels
			UserLocal userLocal = userLocalHome.findByPrimaryKey(primaryKey);
			
			//return the book value object
			//das book value Objekt zur點kgeben
			return userLocal.getUserValue();
			
		} catch (NamingException e) {
			throw new EJBException(e.getMessage());
		} catch (FinderException e) {
			throw new EJBException(e.getMessage());
		} 
	}
	
	/**
	 * This method remove a user by primary key
	 * 
	 * Diese Methode l鰏cht einen Benutzer
	 * 
	 * @ejb.interface-method view-type = "both"
	 */
	public void removeUserByPrimaryKey(Integer primaryKey) throws EJBException {
		
		try {
			InitialContext context = new InitialContext();
			
			//Get the home interface with JNDI from the application server
			//Home Interface 黚er JNDI beim Appliacation Server holen
			UserLocalHome userLocalHome = (UserLocalHome)context.lookup(UserLocalHome.JNDI_NAME);
			
			//find the user by primary key
			//suche den Benutzer anhand seines Prim鋜schl黶sels
			UserLocal userLocal = userLocalHome.findByPrimaryKey(primaryKey);
			
			//remove the user
			//l鰏chen des Benutzer
			userLocal.remove();
			
		} catch (RemoveException e) {
			throw new EJBException(e.getMessage());
		} catch (NamingException e) {
			throw new EJBException(e.getMessage());
		} catch (FinderException e) {
			throw new EJBException(e.getMessage());
		}
	}
	
	/**
	 * This methode update or add a user.
	 * 
	 * Diese Methode aktualisiert oder legt einen neuen Benutzer an.
	 * 
	 * @ejb.interface-method view-type = "both"
	 */
	public void saveUser(UserValue userValue) throws EJBException {
		try {
			InitialContext context = new InitialContext();
			//Get the home interface with JNDI from the application server
			//Home Interface 黚er JNDI beim Appliacation Server holen
			UserLocalHome userLocalHome = (UserLocalHome)context.lookup(UserLocalHome.JNDI_NAME);
			
			//check if the user can be found for update, otherwise insert it as a new user
			//黚erpr黤en ob der Benutzer f黵 die Aktualisierung gefunden wird, ansonsten erstelle einen Neuen
			UserLocal userLocal = null;
			try {
				if(userValue.getId()==null)userValue.setId(new Integer(0));
				userLocal = userLocalHome.findByPrimaryKey(userValue.getId());
			} catch (FinderException e1) {
				userLocal = userLocalHome.create();
			}
			
			//set the value object of the local interface
			userLocal.setUserValue(userValue);
			
		} catch (NamingException e) {
			throw new EJBException(e.getMessage());
		} catch (CreateException e) {
			throw new EJBException(e.getMessage());
		}
	}
	
	
	/** The session context */
	private SessionContext context;

	public UserSession() {
		// TODO Auto-generated constructor stub
	}

	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	public void ejbPassivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	public void ejbRemove() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/**
	 * Set the associated session context. The container calls this method 
	 * after the instance creation.
	 * 
	 * The enterprise bean instance should store the reference to the context 
	 * object in an instance variable.
	 * 
	 * This method is called with no transaction context. 
	 * 
	 * @throws EJBException Thrown if method fails due to system-level error.
	 */
	public void setSessionContext(SessionContext newContext)
		throws EJBException {
		context = newContext;
	}

	/**
	 * An example business method
	 *
	 * @ejb.interface-method view-type = "both"
	 * 
	 * @throws EJBException Thrown if method fails due to system-level error.
	 */
	public void replaceWithRealBusinessMethod() throws EJBException {
		// rename and start putting your business logic here
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -