8 Replies Latest reply on Sep 17, 2010 8:09 AM by sadiquekatihar

    persistence problem

    sadiquekatihar
      I have an object of entity class and this object is fully instantiated but when i try to persist this using Entity Manager it get failed to persist the data into table.
      what is the actual way of persisting a entity object into table.
      ~~
        • 1. Re: persistence problem
          sappo

          Hi sadique,


          could you post your persisting code and the error message if you get one?

          • 2. Re: persistence problem
            sadiquekatihar
            this is my entity
            @Entity
            @Table(name = "authentication_information")
            public class AuthenticationInformation implements java.io.Serializable {

                    private int id;
                    private Person person;
                    private String openid;
                    private String openidProvider;
                    private String firstName;
                    private String lastName;
                    private String phoneNumber;
                    private String email;
                    private String password;
                    private Date createDate;
                    private Date lastUpdtDate;
                    private String createUserId;
                    private String lastUpdtUserId;
                    private String activationKey;
                    private Long createdOn;
                    private Boolean active;

                    public AuthenticationInformation() {
                    }

                    public AuthenticationInformation(int id, Person person) {
                            this.id = id;
                            this.person = person;
                    }
                    public AuthenticationInformation(int id, Person person, String openid,
                                    String openidProvider, String firstName, String lastName,
                                    String phoneNumber, String email, String password, Date createDate,
                                    Date lastUpdtDate, String createUserId, String lastUpdtUserId,
                                    String activationKey, Long createdOn, Boolean active) {
                            this.id = id;
                            this.person = person;
                            this.openid = openid;
                            this.openidProvider = openidProvider;
                            this.firstName = firstName;
                            this.lastName = lastName;
                            this.phoneNumber = phoneNumber;
                            this.email = email;
                            this.password = password;
                            this.createDate = createDate;
                            this.lastUpdtDate = lastUpdtDate;
                            this.createUserId = createUserId;
                            this.lastUpdtUserId = lastUpdtUserId;
                            this.activationKey = activationKey;
                            this.createdOn = createdOn;
                            this.active = active;
                    }

                    @Id
                    @Column(name = "id", unique = true, nullable = false)
                    public int getId() {
                            return this.id;
                    }

                    public void setId(int id) {
                            this.id = id;
                    }

                    @ManyToOne(fetch = FetchType.LAZY)
                    @JoinColumn(name = "person_sequence_number", nullable = false)
                    @NotNull
                    public Person getPerson() {
                            return this.person;
                    }

                    public void setPerson(Person person) {
                            this.person = person;
                    }

                    @Column(name = "openid", length = 50)
                    @Length(max = 50)
                    public String getOpenid() {
                            return this.openid;
                    }

                    public void setOpenid(String openid) {
                            this.openid = openid;
                    }

                    @Column(name = "openid_provider", length = 50)
                    @Length(max = 50)
                    public String getOpenidProvider() {
                            return this.openidProvider;
                    }

                    public void setOpenidProvider(String openidProvider) {
                            this.openidProvider = openidProvider;
                    }

                    @Column(name = "first_name", length = 50)
                    @Length(max = 50)
                    @Pattern(regex = "^[a-zA-Z\\d_]{4,12}$",message = "{invalid_screen_name}")
                    public String getFirstName() {
                            return this.firstName;
                    }

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

                    @Column(name = "last_name", length = 50)
                    @Length(max = 50)
                    public String getLastName() {
                            return this.lastName;
                    }

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

                    @Column(name = "phone_number", length = 50)
                    @Length(max = 50)
                    public String getPhoneNumber() {
                            return this.phoneNumber;
                    }

                    public void setPhoneNumber(String phoneNumber) {
                            this.phoneNumber = phoneNumber;
                    }

                    @Column(name = "email", length = 50)
                    @Length(max = 50)
                    public String getEmail() {
                            return this.email;
                    }

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

                    @Column(name = "password", length = 50)
                    @Length(max = 50)
                    public String getPassword() {
                            return this.password;
                    }

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

                    @Temporal(TemporalType.DATE)
                    @Column(name = "create_date", length = 13)
                    public Date getCreateDate() {
                            return this.createDate;
                    }

                    public void setCreateDate(Date createDate) {
                            this.createDate = createDate;
                    }

                    @Temporal(TemporalType.DATE)
                    @Column(name = "last_updt_date", length = 13)
                    public Date getLastUpdtDate() {
                            return this.lastUpdtDate;
                    }

                    public void setLastUpdtDate(Date lastUpdtDate) {
                            this.lastUpdtDate = lastUpdtDate;
                    }

                    @Column(name = "create_user_id", length = 50)
                    @Length(max = 50)
                    public String getCreateUserId() {
                            return this.createUserId;
                    }

                    public void setCreateUserId(String createUserId) {
                            this.createUserId = createUserId;
                    }

                    @Column(name = "last_updt_user_id", length = 50)
                    @Length(max = 50)
                    public String getLastUpdtUserId() {
                            return this.lastUpdtUserId;
                    }

                    public void setLastUpdtUserId(String lastUpdtUserId) {
                            this.lastUpdtUserId = lastUpdtUserId;
                    }

                    @Column(name = "activation_key", length = 50)
                    @Length(max = 50)
                    public String getActivationKey() {
                            return this.activationKey;
                    }

                    public void setActivationKey(String activationKey) {
                            this.activationKey = activationKey;
                    }

                    @Column(name = "active")
                    public Boolean getActive() {
                            return this.active;
                    }

                    public void setActive(Boolean active) {
                            this.active = active;
                    }
                   
                            @Column(name = "created_on", precision = 17, scale = 17)
                    public Long getCreatedOn() {
                            return this.createdOn;
                    }

                    public void setCreatedOn(Long createdOn) {
                            this.createdOn = createdOn;
                    }
                   

            }


            this is my session bean


            @Name("guest")
            @Scope(ScopeType.EVENT)
            public class GuestSupport extends
                                    EntityHome<AuthenticationInformation>  {

                @Logger private Log log;
                @In private EntityManager entityManager;
                //@In private FacesSupport facesSupport;
                @In private Credentials credentials;
                @In(required = false) private PasswordSupport passwordSupport;
                @In(create=true) private StatusMessages statusMessages;

                @Out(required=false, scope=ScopeType.SESSION)
                private AuthenticationInformation currentUser;
               
               //@PersistenceContext    private EntityManagerFactory emf;

               
               
                @In(create = true)
                    PersonHome personHome;

                private AuthenticationInformation newUser;
            private boolean agreedToTermsOfUse = false;

                @NotEmpty
                @Length(min=4,max=12)
                @Pattern(regex="^[a-zA-Z\\d_]{4,12}$", message="{invalid_screen_name}")
                private String registrationScreenName;


                    //    **************************** Home Code
                    public void setAuthenticationInformationId(Integer id) {
                            setId(id);
                    }

                    public Integer getAuthenticationInformationId() {
                            return (Integer) getId();
                    }

                    @Override
                    protected AuthenticationInformation createInstance() {
                            AuthenticationInformation authenticationInformation = new AuthenticationInformation();
                            return authenticationInformation;
                    }

                    public void load() {
                            if (isIdDefined()) {
                                    wire();
                            }
                    }

                    public void wire() {
                            getInstance();
                            Person person = personHome.getDefinedInstance();
                            if (person != null) {
                                    getInstance().setPerson(person);
                            }
                    }

                    public boolean isWired() {
                            if (getInstance().getPerson() == null)
                                    return false;
                            return true;
                    }

                    public AuthenticationInformation getDefinedInstance() {
                            return isIdDefined() ? getInstance() : null;
                    }
            @Transactional
                public void doRegister() {

               
                   
                    if (!passwordSupport.isConfirmed()) {
                        statusMessages.addToControlFromResourceBundle("password", ERROR, "password_not_confirmed");
                        return;
                    }
                    if (!agreedToTermsOfUse) {
                        statusMessages.addToControlFromResourceBundle("termsOfUseLink", ERROR, "please_agree_to_terms");
                        return;
                    }
            try {
            newUser.setActive(false);
                        //newUser.setPassword(passwordSupport.getPasswordHash(this.registrationScreenName));
                        newUser.setPassword(passwordSupport.getPassword());
                        newUser.setActivationKey(getMD5Hash(newUser.getLastName()+newUser.getCreateUserId()+newUser.getEmail()+newUser.getFirstName()+System.currentTimeMillis()));
                        newUser.setCreatedOn(System.currentTimeMillis());
                       
                        if (newUser==null)
                        {
                            statusMessages.addToControlFromResourceBundle("password", ERROR, "your entity is null");
                        }
                        else
                        {
                           // statusMessages.addToControlFromResourceBundle("save", ERROR, newUser.getFirstName() + newUser.getLastName() + newUser.getActivationKey());
                          
                         entityManager.persist(newUser);
                       String s = "created" + " --" + newUser.getActivationKey() + " --" + newUser.getCreateUserId() + " " +  newUser.getEmail() + " " +  newUser.getPassword() + " " + newUser.getFirstName() + " " +  newUser.getLastName() + " " + newUser.getPerson().getId();
                       statusMessages.addToControlFromResourceBundle("registerButton", ERROR, s);
                      
                        }
              } catch (Exception exc) {
                        log.error("Registration failed for {0}", exc, String.valueOf(this.registrationScreenName));
                        // NOTE: we don't return the Exception message back to the newUser because it may reveal too much information to a hacker
                        statusMessages.addToControlFromResourceBundle("registerButton", ERROR, "general_reg_error");
                        statusMessages.addToControlFromResourceBundle("registerButton", ERROR, exc.getStackTrace().toString());
                    }
                  
                 }
            }

            when i request for doRisgter() it executes but failed to persist data into table
            • 3. Re: persistence problem
              lvdberg

              Hi,


              persisting data in a DB is done through the entityManager. The simplest step is adding entityManager.persist(newUser);


              and add the following annotation to the bean: @In EntityManager entityManager;


              I can't check the rest of your code, but normally this will do the trick.



              Leo


              Please read the documentation of JPA/Hibernate before starting with Seam and persistency!

              • 4. Re: persistence problem
                sadiquekatihar

                i have done so but still it get failed

                • 5. Re: persistence problem
                  lvdberg

                  Hi,


                  sorry I missed the fact that you're already suing the entityHome, so there is no need to use the entitymanager which is included for free.


                  Because you want to add some additional logic before saving the entity, you should override the persist and update methods of home and put your logic in that method and than call super.persist(); Also a EntityHome component should have a ONVERSATION scope to keep track of its instance while you going through the different views.


                  Leo


                  • 6. Re: persistence problem
                    sadiquekatihar

                    could you please write the code for overriding the persistence method for me. because i am confused that how to write it.

                    • 7. Re: persistence problem
                      lvdberg

                      Hi,


                      The following is a simple override. To persist the instance you should call persist (or the overriden update method).
                      Be aware that you can select between persist and update with the isManaged() method.



                      @Override
                      public String persist(){
                                
                      // Her you put your code to add stuff to your instance. Remind you should add everything to the object returnded from getInstance(); !!
                      // so : getInstance().setPassword(passwordSupport.getPassword());
                      //      getInstance().setActivationKey(getMD5Hash(newUser.getLasts....
                      return super.persist();
                      }



                      Leo

                      • 8. Re: persistence problem
                        sadiquekatihar

                        thank you so much for this . i get succeed