[jboss-user] [EJB 3.0] - Re: Foreign key error
neisann
do-not-reply at jboss.com
Tue Aug 14 14:24:14 EDT 2007
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
More information about the jboss-user
mailing list