[EJB 3.0] - Re: Foreign key error
by neisann
Hi,
I added the Column(name =?id?) annotation before the getId() method of Person and Address ejb but I?m getting the same error message about the foreign key violation. My Oracle tables have the following fields:
* Person
Name Type Nullable Default
-------- ------------- -------- -------
ID NUMBER
NAME VARCHAR2(80) Y
ADDRESSID NUMBER(10) Y
EMAIL VARCHAR2(80) Y
PASSWORD VARCHAR2(64) Y
GENDER VARCHAR2(20) Y NULL
...and some more fields
The field ID is the primary key and ADDRESSID is a foreign key.
* Address
Name Type Nullable Default
-------- ------------- -------- -------
ID NUMBER(10)
LINE1 VARCHAR2(255) Y NULL
COUNTRY VARCHAR2(150) Y NULL
POSTCODE VARCHAR2(50) Y NULL
The field ID is the primary key
I have two entity beans called Person and Address.
The data of the tables is entered using the register.jsp page that uses the Register.java class to save the contents to the tables as showed below:
| <f:facet name="footer">
| <h:panelGroup>
| <h:commandButton value="#{msg.submit}"
| action="#{registrationBean.register}" />
| <h:commandButton value="#{msg.reset}" type="reset"/>
| </h:panelGroup>
| </f:facet>
|
* Register.java:
| public String register() throws Exception {
| String toReturn = "failure";
|
| if (validateData()) {
| try {
| // save locale information, in case the user chose a language on the welcome page
| Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
| person.setLocaleCountry(locale.getCountry());
| person.setLocaleLanguage(locale.getLanguage());
|
| Context context = new InitialContext();
| EntityFacade entities = (EntityFacade) context.lookup("EntityFacadeBean/remote");
| person = entities.createPerson(person);
| toReturn = "success";
| }
| catch (PersonEntityExistsException exist) {
| MessageFactory msg = new MessageFactory();
| FacesContext ctx = FacesContext.getCurrentInstance();
|
| ctx.addMessage("registerForm:email",
| new FacesMessage(FacesMessage.SEVERITY_ERROR,
| msg.getMessage("errorEmailExists"), null));
| }
| }
| return toReturn;
| }
|
| @Stateless
| public class EntityFacadeBean implements EntityFacade{
| @PersistenceContext(unitName="shoestringPU") EntityManager em;
|
| public Person getPerson(String email) {
| Person entity = null;
| try {
| Query query = em.createQuery("SELECT p FROM Person p WHERE p.email = ?1");
| query.setParameter(1, email);
| entity = (Person) query.getSingleResult();
| }
| catch (NoResultException noneFound) {
| // if not found, just return null
| }
| return entity;
| }
| /**
| * Perform last minute validation, then if OK save
| * entities (Person and Address in Person)
| * @param toCreate person record to persist
| * @return person record just created, with the
| * primary key set to that just insrted.
| * @throws PersonEntityExistsException if email given exists
| * @throws PersonPasswordException if password is < 6 characters long
| * @throws PersonEmailException if email is blank or null
| */
| public Person createPerson(Person toCreate)
| throws PersonEntityExistsException, PersonPasswordException,
| PersonEmailException {
| String email = toCreate.getEmail();
| if (email == null || email.trim().length() == 0) {
| throw new PersonEmailException("Length is zero");
| }
| String password = toCreate.getPassword();
| if (password == null || password.length() < 6) {
| throw new PersonPasswordException("Length is less than 6");
| }
| if (getPerson(email) != null) {
| throw new PersonEntityExistsException(
| "Person record already exists with an email of " +
| toCreate.getEmail());
| }
| em.persist(toCreate);
| return toCreate;
| }
|
| }
|
The EntityFacadeBean uses a EntityManager to save the records in the table and the annotations in the beans are:
* Person.java
| @Entity
| @SequenceGenerator(name = "PERSON_SEQ", sequenceName = "PERSON_SEQ")
|
| @Id
| @Column(name="id")
| @GeneratedValue(strategy=GenerationType.AUTO, generator = "PERSON_SEQ")
| public int getId() {
| return id;
| }
|
| @ManyToOne(cascade=CascadeType.ALL)
| @JoinColumn(name="addressId",
| referencedColumnName="id")
| public Address getAddress() {
| return address;
| }
|
* Address.java
| @Id
| @Column(name="id")
| @GeneratedValue(strategy=GenerationType.AUTO, generator = "ADDRESS_SEQ")
| public int getId() {
| return id;
| }
|
The entity manager is responsible for recording the informations in the database, then shouldn?t it be recording the registers in the correct order: the Address first and the Person data after that?
Thank you.
Nei
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4074141#4074141
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4074141
18Â years, 11Â months
[JBoss Seam] - Re: feature idea - conversation names
by ellenzhao
This is a nice feature, would be very useful for people who are doing a lot of view templating. I have a page called foo-detail.xhtml. This foo-detail page has CRUD buttons, has its own (nested=true) conversations and the page accessable from many different sources like all-foos.xhtml, all-bars.xhtml --> a-use-case-which-has-foo.xhtml-->foo-detail.xhtml, yet-another-use-case.xhtml --> foo-detail.xhtml, etc. Each source page semantically maps a use case (long-running conversation). When the user click "foo stuff done" button on the foo-detail.xhtml, I'd like it to redirect to the source page which directed to foo-detail.xhtml. It's not that straight forward to do with pages.xhtml, since the button is always on the same page (foo-detail.xhtml) and the action is always from the same conversation bean (fooManager). For now the workaround is that I keep a mother conversation name as a private String field in the fooManager, then use the navigation rule in the pages.xhtml to redirect to right mother page.
If would be great if I could say something like:
<page view-id="foo-detail.xhtml">
<navigation from-action="#{fooManager.fooStuffDone}">
<rule if mother conversation is not null>
<redirect to the page where the mother conversation was at />
without manually writing the references in my manager beans.
But then, it's a bit like jPDL ....
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4074135#4074135
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4074135
18Â years, 11Â months
[JBoss jBPM] - Re: db error : exception handler cannot be loaded and infini
by gogoasa
Well, the error handler can do its job inside a try-catch(Exception) and be sure that nothing escapes it. That's what I personally do, in fact.
But if the error handler itself is not available, then it is more like a "framework" problem. Moreover, the exception handler is typically loaded when there IS a problem. One of possible problems may be a database problem, irrespective of how much effort has been put into the correct configuration of the application.
Indeed, loading all classes in advance may be overkill for processes with lots of custom actions and code deployed in the process archive (although I understand deploying lots of code into the database is actually discouraged and I understand why) -- But, leaving ordinary classes aside, how many exception handlers can there be ?
Eager loading or not -- the actual big problem that occurs on general db failure is the infinite loop. Indeed, all that an already-loaded exception handler can do on general db failure is log stuff to log4j but anyway things get logged. So the only real problem remains the loop.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4074133#4074133
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4074133
18Â years, 11Â months