在你的EJB3项目中使用JPA+visual web app

[color=orange]参考很多资料,结合自己感受写的
最近看了些英语的东西,写写英文试着玩~~
若有语法错误敬请指正,若有技术错误欢迎交流[/color]


环境:netbeans+sun java application server

新建项目:
New project -> Enterprise -> Enterprise Application
Next
Give your project the name,for example ,my project name is "EJBtest"
[color=blue]ps:if you have set up the visual web tookit and you want your web moduleto be a visual web project , don't select the "Create the webapplication module"!
[/color]
Finish~~

新建数据库连接:
Many articles say "using the sample/jdbc"
But I find it uncomfortable ,for I don't even know the password of the database!
If I have my own DB , I can check whether my command is executed or not~~
So I prefer you to setup your DB

Choose "tools" on the top
"java DB database"->"Create a javaDB database"
Enter your new DB'name and you password,mine is "MyNewDB"
"ok"! you have had your own DB!(you can find it at "runtime"->"Databases")

新建持久化单元:
Let's turn back to your project.

Right click your project's EJB module -> "new" -> "persistence unit"
Give your persistence unit a name , I use the default name.
Choose your data source , you will find your own DB there~~then,use it!
Finish!

新建实体bean:
Right click your project's EJB module -> "new" -> "Entity bean"
Enter your name~~
Choose a package or enter a new package
Finish!

Then you'll see your entity bean in a netbeans. Add some properties in your bean ,like
[color=violet]@Column(name = "UserName",length=32)
private String username;
private String pwd;[/color]
[color=blue]ps:if you don't understand the [size=-1]annotation, then don't care about that ~~ JPA will use your class name as yourtable's name and your property's name as your column's name.


ps:when you add some code in your project inport the package needed as the IDE say~~[/color]

Right click your code -> "refactor" -> "Encapsulate fields..."
check whether your new property is seleced
next
Look at the bottom , "do refactor"
then you will find your getter and setter

新建Session Bean:
Right click your project's EJB module -> "new" -> "Session bean for entity classes"
Choose your entity bean , "add"!
Choose a package
If you want it to have remote ability ,select "remote"
Finish!

You will see your "XXXXFacade" "XXXXFacadeLocal" and "XXXXFacadeRemote".
The IDE has already generate the base code.
You can use it at you will at the right time~~

新建Message Driven Bean:
Right click your project's EJB module -> "new" -> "Message Driven Bean"
Enter your name~~
Choose a package or enter a new package
Finish!

You will see the "Message Driven Bean" code and "onMessage" and "persist" method
Add the code below
[color=violet]@PersistenceContext
private EntityManager em;
@Resource
private MessageDrivenContext mdc;[/color]
Chang your "persist" method like this
[color=violet]public void save(Object object)
{
// TODO:
em.persist(object);
//em.flush();
}[/color]
Chang your "onMessage" method like this
[color=violet]public void onMessage(Message message)
{
ObjectMessage msg = null;
try
{
if (message instanceof ObjectMessage)
{
msg = (ObjectMessage) message;
NewsEntity e = (NewsEntity) msg.getObject();
save(e);
}
}
catch (JMSException e)
{
e.printStackTrace();

mdc.setRollbackOnly();
}
catch (Throwable te)
{

te.printStackTrace();
}
}[/color]
[color=blue]ps: If you want to see your result in the DB immediately ,uncomment the code
//em.flush();[/color]

测试你的EJB:
New project -> Web -> Visual web application
Give it a name ,I use "EJBtest-web"
Finish!

Right click your EJB application "EJBtest"
Add javaEE module
Choose "EJBtest-web"

Drag a "Label"s , a "text field" and a "buttom" into your page
double click the buttom to add the event handler code~~
Add the code below to the page1 class
[color=violet]@Resource(mappedName="jms/NewMessageFactory")
private ConnectionFactory connectionFactory;
@Resource(mappedName="jms/NewMessage")
private Queue queue;[/color]
Add the code below to the "button1_action()"method
[color=violet]String name = (String) this.textField1.getText();


if(name != null)
{
Connection connection = null;
MessageProducer messageProducer = null;
Session session;


try
{
connection = connectionFactory.createConnection();


session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
messageProducer = session.createProducer(queue);

ObjectMessage message = session.createObjectMessage();

NewsEntity e = new NewsEntity();
e.setUsername(name);


message.setObject(e);
messageProducer.send(message);


messageProducer.close();
connection.close();


this.label1.setText("ok");
}
catch (JMSException ex)
{
ex.printStackTrace();
}
}[/color]

Then~~~~~~Run your project and see the result~~~~~~~~~~~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值