簡體   English   中英

Java / Spring-JPA持久性列表

[英]Java/Spring - JPA persistence of a list

我對實體文件的持久性有疑問。 問題是:當我保存正確填充了實體文件(在調試模式下檢查)的實體文件時,該文件的文件列表(在這種情況下將是文件夾)不會保留。

檔案:

public class File implements Serializable {

private static final long serialVersionUID = 1L;

@NotNull
@Size(min = 1, max = 25)
private String externalLink;

// Id
private Integer idFile;

private List<File> listOfFile;

@NotNull
@Size(min = 1, max = 255)
private String location;

@NotNull
@Size(min = 1, max = 255)
private String name;

@NotNull
@Size(min = 1, max = 25)
private String size;

// ----------------------------------------------------------------------
// ENTITY DATA FIELDS
// ----------------------------------------------------------------------
@NotNull
@Size(min = 1, max = 255)
private String type;

private Users user;

//GETTERS & SETTERS

}

文件實體:

@Entity
@Table(name = "file", schema = "public")
@NamedQueries({ @NamedQuery(name = "FileEntity.countAll", query = "SELECT 
COUNT(x) FROM FileEntity x") })
public class FileEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Column(name = "externalLink", nullable = true, length = 25)
private String externalLink;

@ManyToOne
@JoinColumn(name = "id_under_file", referencedColumnName = "id_file")
private FileEntity file;

// ----------------------------------------------------------------------
// ENTITY PRIMARY KEY ( BASED ON A SINGLE FIELD )
// ----------------------------------------------------------------------
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id_file", nullable = false)
private Integer idFile;

@OneToMany(mappedBy = "file", targetEntity = FileEntity.class)
private List<FileEntity> listOfFile;

@OneToMany(mappedBy = "file", targetEntity = UsersEntity.class)
private List<UsersEntity> listOfUsers;

@Column(name = "location", nullable = false, length = 255)
private String location;

// "idUser" (column "id_user") is not defined by itself because used as FK
// in a link
// "idUnderFile" (column "id_under_file") is not defined by itself because
// used as FK in a link

@Column(name = "name", nullable = false, length = 255)
private String name;

@Column(name = "size", nullable = false, length = 25)
private String size;

// ----------------------------------------------------------------------
// ENTITY DATA FIELDS
// ----------------------------------------------------------------------
@Column(name = "type", nullable = false, length = 255)
private String type;

// ----------------------------------------------------------------------
// ENTITY LINKS ( RELATIONSHIP )
// ----------------------------------------------------------------------
@ManyToOne
@JoinColumn(name = "id_user", referencedColumnName = "id_user")
private UsersEntity users;

//GETTERS & SETTERS
}

我使用Telosys工具生成CRUD,並使用PostgreSQL。

謝謝,

ServiceImpl:

@Override
    public File create(final File file) {
        final FileEntity fileEntity = new FileEntity();
        this.fileServiceMapper.mapFileToFileEntity(file, fileEntity);
        final FileEntity fileEntitySaved = this.fileJpaRepository
                .save(fileEntity);
        return this.fileServiceMapper.mapFileEntityToFile(fileEntitySaved);
    }

服務映射器:

public File mapFileEntityToFile(final FileEntity fileEntity) {
        final File file = this.mapFileEntityToFileTree(fileEntity);

        // --- Link mapping ( link to Users )
        if (fileEntity.getUsers() != null) {
            final Users user = new Users();
            this.map(fileEntity.getUsers(), user);
            file.setUser(user);
        }
        return file;
    }

    /**
     * Mapping from 'FileEntity' to 'File'
     *
     * @param fileEntity
     */
    public File mapFileEntityToFileTree(final FileEntity fileEntity) {
        if (fileEntity == null) {
            return null;
        }

        // --- Generic mapping
        final File file = this.map(fileEntity, File.class);

        // --- Link mapping ( link to File )
        if (fileEntity.getListOfFile() != null
                && !fileEntity.getListOfFile().isEmpty()) {
            final List<File> files = new ArrayList<File>();
            for (final FileEntity oneFileEntity : fileEntity.getListOfFile()) {
                final File oneFile = new File();
                this.map(oneFileEntity, oneFile);
                files.add(oneFile);
            }
            file.setListOfFile(files);
        }
        return file;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM