반응형
WebLogic EJBGen을 이용해 CMP를 만들 때 Oracle Sequence 를 사용하는 방법입니다.

Eclipse 3.4.2 + Oracle WebLogic Server Support 1.0.0.2 에서 테스트 했습니다.

스키마는 간단하게...

CREATE TABLE INFO (
   SEQ NUMBER PRIMARY KEY,
   DATA VARCHAR(200) NOT NULL
);

CREATE SEQUENCE INFO_SEQ INCREMENT BY 1;


WebLogic 프로젝트 만드는 법은 여기를 참조하시구요~ -_-+

시퀀스를 그려봤는데 당연히 틀렸겠죠? -_-



문제는 SessionBean에서 EntityBean한테 create() 할 때 시퀀스를 적용시켜야 한다는거죵~

InfoBean.java - Entity Bean

package ejb;

import javax.ejb.EntityBean;
import javax.ejb.CreateException;
import weblogic.ejb.GenericEntityBean;
import weblogic.ejbgen.AutomaticKeyGeneration;
import weblogic.ejbgen.Entity;
import weblogic.ejbgen.JndiName;
import weblogic.ejbgen.FileGeneration;
import weblogic.ejbgen.Constants;
import weblogic.ejbgen.CmpField;
import weblogic.ejbgen.LocalMethod;
import weblogic.ejbgen.AutomaticKeyGeneration.AutomaticKeyGenerationType;
import weblogic.ejbgen.CmpField.ColumnType;
import weblogic.ejbgen.Entity.DatabaseType;

@Entity (
  ejbName = "InfoBean",
  dataSourceName = "ejbTest",
  databaseType = DatabaseType.ORACLE,
  tableName = "info",
  primKeyClass = "java.lang.Integer"
)

@JndiName(local = "ejb.Info")

@FileGeneration(
  localClass = Constants.Bool.TRUE, localClassName = "Info",
  localHome = Constants.Bool.TRUE, localHomeName = "InfoHome",
  remoteClass = Constants.Bool.FALSE, remoteHome = Constants.Bool.FALSE,
  valueClass = Constants.Bool.FALSE
)

// Sequence 설정 부분
@AutomaticKeyGeneration(
  type = AutomaticKeyGenerationType.SEQUENCE,
  name = "info_seq",
  cacheSize = "1"
)

abstract public class InfoBean extends GenericEntityBean implements EntityBean
{
  private static final long serialVersionUID = 1L;

  public java.lang.Integer ejbCreate(java.lang.String data) throws CreateException
  {
    // setSeq(seq)
    setData(data);
    
    return null;
  }

  public void ejbPostCreate(java.lang.String data) { }

  @CmpField(column = "seq", primkeyField = Constants.Bool.TRUE, columnType = ColumnType.LONG_STRING)
  @LocalMethod
  public abstract java.lang.Integer getSeq();
  public abstract void setSeq(java.lang.Integer seq);

  @CmpField(column = "data", columnType = ColumnType.LONG_STRING)
  @LocalMethod
  public abstract java.lang.String getData();
  @LocalMethod
  public abstract void setData(java.lang.String data);
}


이렇게 Entity Bean 을 만들고, mysql 이나 mssql 에서 쓰듯이 시퀀스 부분 빼고 데이터를 넣어주면 자동으로 번호가 생성됩니다.

test.jsp - 클라이언트

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page import="javax.naming.*, javax.rmi.*, ejb.*, java.util.Properties" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>

<%
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");

Context ctx = new InitialContext(p);
Object h = ctx.lookup("ejb.InfoManage");
InfoManageHome home = (InfoManageHome)PortableRemoteObject.narrow(h, InfoManageHome.class);
InfoManage im = home.create();

// 테이터1 등록
InfoData info1 = im.insert("테이터1");
out.print(info1.getSeq() + " - " + info1.getData() + "<br>");

//테이터2 등록
InfoData info2 = im.insert("테이터2");
out.print(info2.getSeq() + " - " + info2.getData() + "<br>");
%>

</body>
</html>


InfoManageBean.java - 세션 빈

package ejb;

import javax.ejb.SessionBean;
import javax.naming.Context;
import javax.naming.InitialContext;

import weblogic.ejb.GenericSessionBean;
import weblogic.ejbgen.RemoteMethod;
import weblogic.ejbgen.Session;
import weblogic.ejbgen.JndiName;
import weblogic.ejbgen.FileGeneration;
import weblogic.ejbgen.Constants;

@Session(ejbName = "InfoMenageBean")
@JndiName(remote = "ejb.InfoManage")
@FileGeneration(
  remoteClass = Constants.Bool.TRUE, remoteClassName = "InfoManage",
  remoteHome = Constants.Bool.TRUE, remoteHomeName = "InfoManageHome",
  localClass = Constants.Bool.FALSE,
  localHome = Constants.Bool.FALSE
)

public class InfoManageBean extends GenericSessionBean implements SessionBean
{
  private static final long serialVersionUID = 1L;

  public void ejbCreate() { }

  @RemoteMethod
  public InfoData insert(String data) throws Exception
  {
    Context ctx = new InitialContext();
    InfoHome home = (InfoHome)ctx.lookup("ejb.Info");
    Info info = home.create(data);

    // DTO 클래스로 변환
    return convertData(info);
  }

  // Info -> InfoData
  private InfoData convertData(Info info)
  {
    return new InfoData(info.getSeq(), info.getData());
  }
}


InfoData.java - Data Transfer Object

package ejb;

public class InfoData implements java.io.Serializable

  private Integer seq;
  private String data;
 
  public InfoData(Integer seq, String data) {
    this.seq = seq;
    this.data = data;
  }
 
  public Integer getSeq() {
    return seq;
  }
  public String getData() {
    return data;
  }
  public void setData(String data) {
    this.data = data;
  }
 
  private static final long serialVersionUID = 1L;
}

반응형

'Server > WebLogic' 카테고리의 다른 글

Eclipse + WebLogic 추가 설정 사항들...  (6) 2011.08.23
웹로직 ZIP 배포판 설치 (윈도우)  (1) 2011.07.14
WebLogic 10.3 JDBC 설정(DataSource)  (13) 2009.04.18
Eclipse + WebLogic 10.3  (17) 2009.03.15
Domain 생성 및 테스트  (1) 2009.03.14
Oracle WebLogic Server 10.3 설치  (6) 2009.03.14
//