我正在使用Wildfly 10。 我正在尝试将实体绑定到jsf页面并将其与jpa保存到我的数据库中。
我得到了 javax.servlet.ServletException:javax.ejb.EJBException:java.lang.IllegalArgumentException:未知实体:de.rupp.model.Contact $ Proxy $ _ $$ _ WeldClientProxy javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
我在WEB-INF / classes / META-INF中的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="contacts">
<!-- If you are running in a production environment, add a managed
data source, this example data source is just for development and testing! -->
<!-- The datasource is deployed as WEB-INF/jsftest-ds.xml, you
can find it in the source at src/main/webapp/WEB-INF/jsftest-ds.xml -->
<jta-data-source>java:/ContactsDS</jta-data-source>
<class>de.rupp.model.Contact</class>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
我的实体
@Named
@RequestScoped
@Entity
@NamedQueries({
@NamedQuery(name=Contact.FIND_ALL, query="SELECT c FROM Contact c"),
@NamedQuery(name=Contact.DELETE_ALL, query="DELETE FROM Contact")
})
public class Contact implements Serializable{
public final static String FIND_ALL = "Contacts.findAll";
public final static String DELETE_ALL = "Contacts.deleteAll";
/**
*
*/
private static final long serialVersionUID = -7050382512059267701L;
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;
private String phone;
private String message;
//getter and setter omitted for brevity
}
我的控制器
@Named
@RequestScoped
public class EingabeController implements Serializable {
private Logger log = LoggerFactory.getLogger(EingabeController.class);
@Inject
private ContactsDAOBean contactsDAOBean;
@Inject
private Contact contact;
public void save() {
this.contactsDAOBean.save(this.contact);
log.info(this.contact + " saved");
}
}
我的持久层工作我为此编写了一个Arquillian测试:
@RunWith(Arquillian.class)
public class ContactsDAOTest {
@Deployment
public static Archive<?> createTestArchive() {
return ShrinkWrap.create(WebArchive.class, "test.war")
.addClasses(Contact.class, AbstractDAO.class, AbstractDAOBean.class, ContactsDAOBean.class)
.addAsResource("META-INF/persistence.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Inject
private ContactsDAOBean contactsDAOBean;
@Before
public void init() {
contactsDAOBean.deleteAll();
}
@Test
public void save() {
Contact contact = new Contact();
contact.setEmail("h.rupp@dzbw.de");
contact.setFirstName("Hans");
contact.setLastName("Rupp");
contact.setPhone("0711 8108 276232");
contact.setMessage("Ich will hier raus");
contactsDAOBean.save(contact);
List<Contact> contacts = contactsDAOBean.findAll();
assertEquals(1, contacts.size());
}
}
非常感谢任何想法
------------------- edit --------------------------- -----------
我重构了这个页面: 最初Contact实体已直接绑定到输入元素,如
<h:inputText value="#{contact.firstName}" styleClass="form-control"/>
我更改了控制器,以便生成要保存的实体。
@Named
@RequestScoped
public class EingabeController implements Serializable {
@Inject
private ContactsDAOBean contactsDAOBean;
@Named
@Produces
private Contact newContact;
@PostConstruct
public void init() {
this.newContact = new Contact();
}
public String save() {
this.contactsDAOBean.save(this.newContact);
log.info(this.newContact + " saved");
this.savedContact = this.newContact;
return "data";
}
@Named
@Produces
private Contact savedContact;
}
@Named和@RequestScoped Annotations已从Contact eintity中删除。
<h:inputText value="#{newContact.lastName}" styleClass="form-control" id="lastName"/>
页面的工作原理如下,可以在没有errro的情况下调用保存操作。
也许真正的原因是我对JSF和CDI应该如何工作的误解。
我认为在原版中 - 在呈现页面时实例化绑定到控件的bean - bean填充了用户输入的值 - 当调用save动作时,bean被注入控制器 这可能是我在推理中的错误:控制器已实例化 使用该页面,并且当时注入了Contacts bean,是不是像绑定到控件的bean那样的实例?
我只是想(重新)学习JSF ....任何人都可以推荐一个很好的来源,详尽讨论JSF应用程序的架构吗? 我想我有足够的书籍可以单独讨论控件等。
答案 0 :(得分:0)
尝试编辑您的实体,如下所示。
@Entity
@Table(name = "nameOfTableInDatabase", catalog = "databaseName")
@NamedQueries({
@NamedQuery(name=Contact.FIND_ALL, query="SELECT c FROM Contact c"),
@NamedQuery(name=Contact.DELETE_ALL, query="DELETE FROM Contact")
})
public class Contact implements java.io.Serializable {
bla
bla
}