简体   繁体   English

如何在Play框架中更新个人资料记录

[英]How do I update my profile record in play framework

I am trying to update my profile module using profile id..but it is not updating..I got the following error: [PersistenceException: ERROR executing DML bindLog[] error[ERROR: multiple assignments to same column "register_id"]] 我正在尝试使用配置文件ID更新我的配置文件模块,但是没有更新。

I attached my controller class..in that edit method I tried the update query 我附加了控制器类..在该编辑方法中,我尝试了更新查询

Controller class: 控制器类:

package controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.*;

import org.apache.commons.mail.EmailException;

import play.Logger;
import play.data.Form;
import play.libs.F.Promise;
import play.libs.OpenID;
import play.libs.OpenID.UserInfo;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.*;

import com.avaje.ebean.Ebean;
import com.typesafe.plugin.MailerAPI;
import com.typesafe.plugin.MailerPlugin;


public class Application extends Controller {
    public static Result index() {

        return ok(index.render(""));
    }

    public static Result signup() {
        return ok(login.render(null));

    }

    public static Result register() {

        Register register = Form.form(Register.class).bindFromRequest().get();
        register.save();
        return ok(index.render(""));
    }

    public static Result login() {
        Register register = Form.form(Register.class).bindFromRequest().get();
        String uname = register.getUsername();
        String pass = register.getPassword();
        List<Register> login = Register.find.where().eq("username", uname)
                .eq("password", pass).findList();
        if (login.isEmpty()) {
            return unauthorized("Please register and try login");
        } else {
            session().clear();
            session("registerId", +login.get(0).getRegisterId() + "");
            return ok(app.render("register"));
             }
    }

    public static Result home() {

        return ok(app.render(""));
    }

    public static Result menu() {
        Register register = Form.form(Register.class).bindFromRequest().get();
        String uname = register.getUsername();
        String pass = register.getPassword();

        List<Register> login = Register.find.where().eq("username", uname)
                .eq("password", pass).findList();

        session("registerId", +login.get(0).getRegisterId() + "");

        String reg=session().get("registerId");
        System.out.println(reg);
        Profile profile1=Profile.find.byId(reg);
        System.out.println(profile1);

        if(profile1!=null)
        {
        return ok(profile.render(profile1));
        }
        else 

            return ok(empty.render(""));
    }
    public static Result edit(int profileId) {



            Form<Profile> profile1 = Form.form(Profile.class).bindFromRequest();
            profile1.get().update(profileId);




        return ok("");
    }
    public static Result logout() {
        return ok(index.render("fhjk"));
    }

    public static Result profile() {
        Profile profile = Form.form(Profile.class).bindFromRequest().get();
        profile.getRegister().setRegisterId(
                Integer.parseInt(session().get("registerId")));

        profile.save();
        return ok();
    }

    public static Result forgotPassword() {

        return ok(forgotPassword.render("eghj"));
    }

    public static Result email() throws EmailException {
        ForgotPassword forgotPass = Form.form(ForgotPassword.class)
                .bindFromRequest().get();
        String emailId = forgotPass.getEmail();
        System.out.println(emailId);
        MailerAPI  mail = play.Play.application().plugin(MailerPlugin.class).email();
           mail.setSubject("mailer");
           mail.setRecipient(emailId);
           mail.setFrom("sindhu.raja90@gmail.com");
           mail.send( "text" );  
           return ok("gg");


    }

    @SuppressWarnings("serial")
    public static final Map<String, String> identifiers = new HashMap<String, String>() {
        {
            put("google", "https://www.google.com/accounts/o8/id");
        }
    };

    @SuppressWarnings("deprecation")
    public static Result auth() {
        Logger.debug("authenticate");
        String providerId = "google";
        String providerUrl = identifiers.get(providerId);
        String returnToUrl = "http://localhost:9000/login/verify";
        if (providerUrl == null) {
            return badRequest("Could not find provider " + providerId);
        }

        Map<String, String> attributes = new HashMap<String, String>();
        attributes.put("Email", "http://schema.openid.net/contact/email");
        attributes
                .put("FirstName", "http://schema.openid.net/namePerson/first");
        attributes.put("LastName", "http://schema.openid.net/namePerson/last");

        Promise<String> redirectUrl = OpenID.redirectURL(providerUrl,
                returnToUrl, attributes);
        return redirect(redirectUrl.get());
    }

    @SuppressWarnings("deprecation")
    public static Result verify() {
        Logger.debug("verifyLogin");
        System.out.println("verify executed");
        Promise<UserInfo> userInfoPromise = OpenID.verifiedId();
        userInfoPromise.get();
        return ok("");
    }

}

Profile.views.html: Profile.views.html:

@(profile1: Profile)
@main("") {
<div class="container test">

    <!-- form: -->
    <section>
        <div class="col-lg-8 col-lg-offset-2">
            <label> <a href="@routes.Application.home()">Home</a>
            </label> 
            <label> <a href="@routes.Application.logout" class="logout">Logout</a>
            </label>

            <div class="page-header">
                <h2>Profile</h2>

            </div>

            <form class="form-horizontal" method="POST" id="profile"
                action="@routes.Application.profile()">
                <div class="input-group">
                    <input type="file" class="file">
                </div>


                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="firstName">First
                            Name:</label>
                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="firstname" value='@profile1.getFirstname'
                                id="firstName" autocomplete="off" required
                                data-bv-notempty-message="The first name is required" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="middleName">Middle
                            Name:</label>
                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="middlename" value='@profile1.getMiddlename'
                                autocomplete="off" id="middleName">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="lastName">Last
                            Name:</label>
                        <div class="col-lg-4">

                            <input type="text" class="form-control" name="lastname" value="@profile1.getLastname" 
                                id="lastName" autocomplete="off" required
                                data-bv-notempty-message="The last name is required" />

                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label">Date of Birth:</label>
                        <div class="col-xs-4">
                            <input type="date" class="form-control" name="dob" id="dob" value="@profile1.getDob"
                                autocomplete="off" data-bv-date="false"
                                data-bv-date-message="The birthday is not valid" required
                                data-bv-notempty-message="The date of birth is required" /> 
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label">Gender:</label>
                        <div class="col-xs-2">
                            <label class="radio-inline"> <input type="radio"
                                name="gender" value="male" required 
                                data-bv-notempty-message="The gender is required" /> Male
                            </label>
                        </div>
                        <div class="col-xs-2">
                            <label class="radio-inline"> <input type="radio"
                                name="gender" value="female"> Female
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="maritalStatus">Marital
                            Status: </label>
                        <div class="col-lg-4">
                            <select class="form-control" name="maritalStatus">

                                <option value="single">Single</option>
                                <option value="married">Married</option>
                                <option value="divorced">Divorced</option>

                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-lg-3 control-label"  for="permanentAddress">Permanent
                            Address:</label>
                        <div class="col-lg-4">
                            <textarea rows="3" class="form-control"  name="permanentAddress"
                                id="permanentAddress" required
                                data-bv-notempty-message="The permanent address is required">@profile1.getPermanentAddress</textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="tempAddress">Temporary
                            Address:</label>
                        <div class="col-lg-4">
                            <textarea rows="3" class="form-control" name="tempAddress" 
                                id="tempAddress" required
                                data-bv-notempty-message="The temporary address is required">@profile1.getTempAddress</textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="bloodGroup">Blood
                            Group:</label>
                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="bloodGroup" 
                                id="bloodGroup" value="@profile1.getBloodGroup" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="idMark">Identification
                            Mark:</label>
                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="identificationMark" value="@profile1.getIdentificationMark"
                                id="idMark" autocomplete="off" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="personalNo">Personal
                            No:</label>
                        <div class="col-lg-4">
                            <input type="tel" class="form-control" name="personalNum" value="@profile1.getPersonalNum" 
                                id="personalNo" autocomplete="off" required
                                data-bv-notempty-message="The phone no is required">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="emergencyNo">Emergency
                            No:</label>
                        <div class="col-lg-4">
                            <input type="tel" class="form-control" name="emergencyNum" value="@profile1.getEmergencyNum"
                                id="emergencyNo" autocomplete="off" required
                                data-bv-notempty-message="The phone no is required">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="location">Location:
                        </label>
                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="location" value="@profile1.getLocation"
                                id="location" autocomplete="off">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="state">State: </label>
                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="state" value="@profile1.getState" id="state">

                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="country">Country:

                        </label>
                        <div class="col-lg-4">
                            <input class="form-control" name="country" value="@profile1.getCountry" id="country"
                                type="text">


                        </div>
                    </div>
                    <div class="form-group">

                        <div class="col-xs-offset-3 col-xs-9">
                            <input type="submit" class="btn btn-primary" id="submit" value="Submit" >
                                </div>
                                                    </div>








            </form>
            <div>
                            <form method="get" action="@routes.Application.edit(profile1.getProfileId)">
                            <input type="submit"  class="btn btn-primary" id="edit" value="edit" onclick="disableTxt()"/>
                            </form>
                             </div>
        </div>
    </section>

</div>



<script type="text/javascript">
        function disableTxt() {

            document.getElementById('country').disabled='true';
    }

</script>
<style type="text/css">
.form-horizontal .control-label {
    text-align: left;
}

.test .input-group .btn-default,.form-control.file-caption,.glyphicon,.close
    {
    display: none;
}

.sample {
    margin-bottom: 50px;
}

.test .btn-file:hover {
    background: #ffffff;
    border: none;
}
</style>
}

Models class: 型号类别:

package models;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import play.db.ebean.Model;

@Entity
@Table(name = "profile")
public class Profile extends Model {

    /**
     * 
     */
    private static final long serialVersionUID = 1L; 

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator ="profile_profile_id_seq")
    private int profileId;


    public int getProfileId() {
        return profileId;
    }

    public void setProfileId(int profileId) {
        this.profileId = profileId;
    }
    @OneToOne
    @JoinColumn(table="register", name= "register_id")
    private Register register;

        private int registerId;
    public Register getRegister() {
        return register;
    }

    public void setRegister(Register register) {
        this.register = register;
    }

    public Profile(){
        register = new Register();
    }
    private byte[] photo;
    @Column(name="firstname")
    private String firstname;
    private String middlename;
    private String lastname;
    private Date   dob;
    private String gender;
    private String maritalStatus;
    private String permanentAddress;
    private String tempAddress;
    private String bloodGroup;
    private String identificationMark;
    private String personalNum;
    private String emergencyNum;
    private String location;
    private String state;
    private String country;



    public byte[] getPhoto() {
        return photo;
    }

    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }

    public String getIdentificationMark() {
        return identificationMark;
    }

    public void setIdentificationMark(String identificationMark) {
        this.identificationMark = identificationMark;
    }

    public String getPersonalNum() {
        return personalNum;
    }

    public void setPersonalNum(String personalNum) {
        this.personalNum = personalNum;
    }

    public String getEmergencyNum() {
        return emergencyNum;
    }

    public void setEmergencyNum(String emergencyNum) {
        this.emergencyNum = emergencyNum;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getMiddlename() {
        return middlename;
    }

    public void setMiddlename(String middlename) {
        this.middlename = middlename;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }



    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getMaritalStatus() {
        return maritalStatus;
    }

    public void setMaritalStatus(String maritalStatus) {
        this.maritalStatus = maritalStatus;
    }

    public String getPermanentAddress() {
        return permanentAddress;
    }

    public void setPermanentAddress(String permanentAddress) {
        this.permanentAddress = permanentAddress;
    }

    public String getTempAddress() {
        return tempAddress;
    }

    public void setTempAddress(String tempAddress) {
        this.tempAddress = tempAddress;
    }

    public String getBloodGroup() {
        return bloodGroup;
    }

    public void setBloodGroup(String bloodGroup) {
        this.bloodGroup = bloodGroup;
    }


    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();


        stringBuilder.append("Profile[Photo=");
        stringBuilder.append(photo);
        stringBuilder.append(",ProfileId=");
        stringBuilder.append(profileId);

        stringBuilder.append(",FirstName=");
        stringBuilder.append(firstname);
        stringBuilder.append(",MiddleName=");
        stringBuilder.append(middlename);
        stringBuilder.append(",LastName=");
        stringBuilder.append(lastname);
        stringBuilder.append(",Dob=");
        stringBuilder.append(dob);
        stringBuilder.append(",Gender=");
        stringBuilder.append(gender);
        stringBuilder.append(",MaritalStatus=");
        stringBuilder.append(maritalStatus);
        stringBuilder.append(",PermanentAddress=");
        stringBuilder.append(permanentAddress);
        stringBuilder.append(",TempAddress=");
        stringBuilder.append(tempAddress);
        stringBuilder.append(",BloodGroup=");
        stringBuilder.append(bloodGroup);
        stringBuilder.append(",IdentificationMark=");
        stringBuilder.append(identificationMark);
        stringBuilder.append(",PersonalNum=");
        stringBuilder.append(personalNum);
        stringBuilder.append(",EmergencyNum=");
        stringBuilder.append(emergencyNum);
        stringBuilder.append(",Location=");
        stringBuilder.append(location);
        stringBuilder.append(",Country=");
        stringBuilder.append(country);
        stringBuilder.append(",State=");
        stringBuilder.append(state);
        stringBuilder.append(",Register Id=");
        stringBuilder.append(register != null ? register.getRegisterId() : "");

        stringBuilder.append("]");
        return stringBuilder.toString();

    }
    public int getRegisterId() {
        return registerId;
    }

    public void setRegisterId(int registerId) {
        this.registerId = registerId;
    }
    public static Finder<String, Profile> find = new Finder<String, Profile>(
            String.class, Profile.class);



}

I believe it's because you have a "register" that is identified as a column named "register_id" and you also have a public getter/setter for "registerId" which will try to use the same "register_id" column. 我相信这是因为您有一个被标识为名为“ register_id”的列的“寄存器”,并且还具有“ registerId”的公共获取器/设置程序,它将尝试使用相同的“ register_id”列。 I suggest renaming registerId to something else, such as "testRegisterId" (and change the public getter/setter to getTestRegisterId and setTestRegisterId). 我建议将registerId重命名为其他名称,例如“ testRegisterId”(并将公用的getter / setter更改为getTestRegisterId和setTestRegisterId)。

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

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