Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在保存MySQL之前获取自动增量id_Mysql_Spring_Spring Mvc - Fatal编程技术网

在保存MySQL之前获取自动增量id

在保存MySQL之前获取自动增量id,mysql,spring,spring-mvc,Mysql,Spring,Spring Mvc,我有一个应用程序,用户必须将信息保存到数据库,将图像保存到文件系统目录,并将图像路径保存到数据库中。我的问题是如何从数据库表中获取ID(自动增量)以在数据库中创建路径。例如:car1200.jpeg(car:image name,1200:Id用户)。 这是我的代码: 控制器: @RequestMapping(value="/save",method=RequestMethod.POST) public String add ( @RequestParam("prix") Long pr

我有一个应用程序,用户必须将信息保存到数据库,将图像保存到文件系统目录,并将图像路径保存到数据库中。我的问题是如何从数据库表中获取ID(自动增量)以在数据库中创建路径。例如:car1200.jpeg(car:image name,1200:Id用户)。 这是我的代码: 控制器:

@RequestMapping(value="/save",method=RequestMethod.POST)
    public String add ( @RequestParam("prix") Long prix, 
            RequestParam("adresse") String ville,
            @RequestParam("categorie") String categorie,
            @RequestParam("photos") MultipartFile file,
            ) throws FileNotFoundException


 {

   String chemin=null;
   if (!file.isEmpty())
     {
      try {
       String orgName = file.getOriginalFilename();
       // this line to retreive just file name 
       String 
       name=orgName.substring(orgName.lastIndexOf("\\")+1,orgName.length());
       chemin="e:\\images\\"+name;// here I want to add ID of the user ,I 
       //don t know how to get since it is auto increment 
       File file1=new File(chemin);
       file.transferTo(file1);
         } catch (IOException e) {
                                    e.printStackTrace();
                                  }

    }
    annonce.setImage(chemin);
    annonce.setTitre(prix);
    annonce.setCorps(ville);
    annonce.setPrix(cetegorie)
    annoncedao.save(annonce);
    return "SuccessAddAnnonce";
}
ANNOCE类:

package com.eBenamar.Entities;
import java.io.Serializable;
import java.sql.Blob;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import org.hibernate.annotations.Cascade;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * @author user
 *
 */
@Entity
public class Annonce implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id @GeneratedValue
    Integer id;
    @NotEmpty
    String titre;
    Long prix;
    @Column(length=10000000)
    byte [] photos;
    public byte[] getPhotos() {
        return photos;
       }
    String ville;
    String categorie;
    @Lob
    @Column(length=10000000)
    byte []   photos;

    public Annonce() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Annonce( String ville, Long prix, String categorie,
            byte[] photos) {
        super();
        this.ville= ville;
        this.prix = prix;
        this.categorie = categorie;
        this.photos = photos;

    }



    public Annonce( String categorie, Long prix, String ville, byte[] photos) {
        super();
        this.ville= ville;
        this.categorie = categorie;
        this.prix = prix;
        this.photos = photos;
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getVille() {
        return ville;
    }
    public void setTVille(String ville) {
        this.ville = ville;
    }
    public String getCategorie() {
        return categorie;
    }
    public void setCategorie(String categorie) {
        this.categorie= categorie;
    }
    public Long getPrix() {
        return prix;
    }
    public void setPrix(Long prix) {
        this.prix = prix;
    }
    public byte[] getPhotos() {
        return photos;
    }
    public void setPhotos(byte[] photos) {
        this.photos = photos;
    }

}

我想获取保存在数据库中的最后一个ID(自动增量)。

为什么要将自动增量用作路径的一部分?我认为在这里做你想做的事情是非常重要的,因为自动增量值通常在插入完成后才可用。假设你想得到的ID实际上是ANNOCE的ID,而不是用户的ID,并且假设ANNOCE确实有一个
image
属性,您需要在不带图像的情况下保存annonce,flush()以确保JPA插入实体并由此生成ID,然后计算chemin并将其存储在图像中。但是,由于无论如何都要在实体中存储映像路径,因此不需要在路径中包含实体的ID。你可以只使用一个序列或uuid,对于图像路径。是否在MYSQL中为列id设置了“自动递增列检查”?JB:问题是,我将图像保存在文件系统中,并且我不希望有两个同名的图像,因此我希望将Annonce id添加到Annonce表中路径中的图像名称中。如果为True,则在保存之前我无法获取id,但可以获取最后一个id一个是因为它是自动递增的,所以我将只使用id++Tanmay:是的,我为MySQL自动递增做了。为什么要使用自动递增作为路径的一部分?我认为在这里做你想做的事情是非常重要的,因为自动增量值通常在插入完成后才可用。假设你想得到的ID实际上是ANNOCE的ID,而不是用户的ID,并且假设ANNOCE确实有一个
image
属性,您需要在不带图像的情况下保存annonce,flush()以确保JPA插入实体并由此生成ID,然后计算chemin并将其存储在图像中。但是,由于无论如何都要在实体中存储映像路径,因此不需要在路径中包含实体的ID。你可以只使用一个序列或uuid,对于图像路径。是否在MYSQL中为列id设置了“自动递增列检查”?JB:问题是,我将图像保存在文件系统中,并且我不希望有两个同名的图像,因此我希望将Annonce id添加到Annonce表中路径中的图像名称中。如果为True,则在保存之前我无法获取id,但可以获取最后一个id一个是因为它是自动增量的,所以我将只使用id++Tanmay:是的,我为MySQL自动增量做了。