JEE5 Sample

From no name for this wiki
Jump to: navigation, search

Download NetBeans Projekt

JSF

EJB Injection

Injection eines EJBs im WebLayer. name ist der JNDI-Name:

@EJB(name="QuestionBCBean") QuestionBCLocal bc;

Definition des Beans:

@Stateless(name="QuestionBCBean")
public class QuestionBCBean implements QuestionBCLocal {

EJB3

Injection

Injection eines Entitymanagers in einem EJB3:

@PersistenceContext(unitName="WebLearingPlatformEAR-ejbPU")
protected EntityManager em;

JPA

persistence.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <persistence version="1.0" 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">
  <persistence-unit name="WebLearingPlatformEAR-ejbPU" transaction-type="JTA">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <jta-data-source>jdbc/sample</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="toplink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
 </persistence>

Bikirektionale One To Many Beziehung

In der Klasse Question.java

    @OneToMany(mappedBy="question", cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    private List<QuestionOption> options = new ArrayList<QuestionOption>();

In der Klasse QuestionOption.java


    @ManyToOne(optional=false, fetch = FetchType.EAGER)
    @JoinColumn(name="questionid", nullable=false)
    private Question question;

Resourcen