简体   繁体   English

EJB Project类映射内的实体Bean

[英]Entity Beans inside EJB Project class mapping

I am using Eclipse and Derby database (with Embeeded Driver). 我正在使用Eclipse和Derby数据库(带有Embeeded驱动程序)。 As a starting point I am running the asadmin (from glassfish) to start-database from there. 作为起点,我从那里运行asadmin(从glassfish)到启动数据库。 The database within eclipse can be pinged, as well as connected to just fine. eclipse中的数据库可以被ping通,也可以连接到数据库。 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. 启动了将会话bean和实体bean结合在一起的EJB项目后,我遇到了以下异常-java.lang.IllegalArgumentException:未知实体bean类:class model.Userbay,请验证该类是否已标有@Entity批注。

Just few lines below this error i get pointed to this line of code - Userbay user = emgr.find(model.Userbay.class, username); 在此错误下方,只有几行指向该行代码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. 尽管我的感觉是persistence.xml可能首先导致了问题。

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; 以下是java / xml文件;

Persistence.xml (which is stored under ejbModule/META-INF) Persistence.xml(存储在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 我也尝试添加以下属性标签-但是,它还会引发另一个错误org.apache.derby.client.am.SqlException:模式'ADRIAN'不存在

<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) userRegistrationSB.java(会话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) Userbay.java(实体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 我也尝试添加以下属性标签-但是,它还会引发另一个错误org.apache.derby.client.am.SqlException:模式'ADRIAN'不存在

Have you checked if your database schema was actually created? 您是否检查过数据库架构是否已创建? If it did not, adding the following lines in your persistence.xml might help. 如果没有,请在persistence.xml中添加以下几行可能会有所帮助。

<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. 您可能还想手动取消部署应用程序,或者如果它是您的开发人员机器,则这是唯一部署的应用程序,只是删除您域中应用程序目录的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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