简体   繁体   中英

Entity Beans inside EJB Project class mapping

I am using Eclipse and Derby database (with Embeeded Driver). As a starting point I am running the asadmin (from glassfish) to start-database from there. The database within eclipse can be pinged, as well as connected to just fine. Having started my EJB project which combines the session beans and entity beans I have ran into the following exception - java.lang.IllegalArgumentException: Unknown entity bean class: class model.Userbay, please verify that this class has been marked with the @Entity annotation.

Just few lines below this error i get pointed to this line of code - Userbay user = emgr.find(model.Userbay.class, username); Although my feeling is that it could be a problem with the persistence.xml that causes it in the first place.

I would really appreciate any hints/help given towards fixing this annoying problem me and my friend are facing for quite a time now..

The following are the java/xml files;

Persistence.xml (which is stored under ejbModule/META-INF)

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
    <persistence-unit name="EJBAuctionv2">
        <class>model.Userbay</class>
        <class>model.Item</class>
        <class>model.Category</class>
    </persistence-unit>
</persistence>

I've also tried adding the following properties tag - however it grants another error org.apache.derby.client.am.SqlException: Schema 'ADRIAN' does not exist

<properties>
     <property name="javax.persistence.jdbc.password" value="test" />
     <property name="javax.persistence.jdbc.user" value="adrian" />
     <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeededDriver" />
     <property name="javax.persistence.jdbc.url" value="jdbc:derby:C:/Users/Adrian/MyDB;create=true" />
    </properties>

userRegistrationSB.java (Session Bean)

package auction;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Remote;
import javax.ejb.Singleton;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import model.Userbay;

/**
 * Session Bean implementation class userRegistrationSB
 */
@Remote @Stateless
public class userRegistrationSB implements userRegistrationSBRemote {

    //@EJB private Userbay user;
    @PersistenceContext private EntityManager emgr;
    /**
     * Default constructor. 
     */
    public userRegistrationSB() {
        // TODO Auto-generated constructor stub
        System.out.println("TEST2");
    }

    @Override
    public boolean registerUser(String username, String password, String email,
            String firstname, String lastname) {
        boolean registered = false;

        System.out.println("Registering an user");
        Userbay user = emgr.find(model.Userbay.class, username);
        if (user != null) {
            System.out.println("Username doesn't exist.");
            registered = true;
        } else {
            registered = false;
            System.out.println("Username already exists.");
        }

        return registered;
    }

    @Override
    public boolean userExists(String username) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean userMatchesPassword(String username, String password) {
        // TODO Auto-generated method stub
        return false;
    }

}

Userbay.java (Entity Bean)

package model;

import java.io.Serializable;
import javax.persistence.*;
import java.util.List;




@Entity @Table (name = "Userbay")
/*@NamedQuery(name="Userbay.findAll", query="SELECT u FROM Userbay u")*/
public class Userbay implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id @Column(name="USER_NAME")
    private String userName;

    private String email;

    @Column(name="FIRST_NAME")
    private String firstName;

    @Column(name="LAST_NAME")
    private String lastName;

    @Column(name="PASSWORD")
    private String password;

    //bi-directional many-to-one association to Item
    @OneToMany(mappedBy="userbay")
    private List<Item> items;

    public Userbay() {
    }

    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Item> getItems() {
        return this.items;
    }

    public void setItems(List<Item> items) {


this.items = items;
}

public Item addItem(Item item) {
    getItems().add(item);
    item.setUserbay(this);

    return item;
}

public Item removeItem(Item item) {
    getItems().remove(item);
    item.setUserbay(null);

    return item;
}

}

I've also tried adding the following properties tag - however it grants another error org.apache.derby.client.am.SqlException: Schema 'ADRIAN' does not exist

Have you checked if your database schema was actually created? If it did not, adding the following lines in your persistence.xml might help.

<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />

You may also want to undeploy your application manually or if it is your developer machine and this is the only application deployed merely delete content of the applications directory in your domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM