English 中文(简体)
translating outer join SQL query into EJB entity beans
原标题:

I m new to EJB and trying to get my head around translating SQL concepts to EJB entity beans.

Suppose I have two tables: PEOPLE (id, name), CONTACT(pid, phone_number). If I want to get a list of all people whether or not they have phone #s, in my EJB session bean I simply issue a SQL query via JDBC such as:

SELECT PEOPLE.name, CONTACT.phone_number 
FROM PEOPLE 
LEFT JOIN CONTACT ON PEOPLE.id = CONTACT.pid

Instead of using SQL/JDBC, I now want to use EJB entity beans. So I create corresponding EJB3 entity bean classes for my tables.

So I now have access to both entity classes from my session bean and I no longer wish to access my database tables directly via SQL/JDBC from my session bean. I only want to use my entity beans and capabilities of JPA. What s the proper EJB design so that in my session bean, I get the same results as my SQL query?

I m unclear about how to use the EJB entity bean classes to produce the same results as my SQL outer join query. Help.

最佳回答

First, create two JPA entities, something like that for People:

@Entity
@Table( name="PEOPLE" )
public class People {

    @Id @Column
    private Long id;

    @Column
    private String name;

    @OneToOne
    @JoinColumn( name="pid" )
    private Contact contact;

    // getters and setters
}

And for Contact:

@Entity
@Table( name="CONTACT" )
public class Contact {

    @Id @Column
    private Long pid;

    @Column
    private String phoneNumber;

    // getters and setters
}

Then, to generate the OUTER JOIN query using JPQL:

SELECT p FROM People p LEFT JOIN p.contact c
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签