如何为可嵌入类生成id?

问题描述 投票:0回答:3

一个注意:许多文件

我怎样才能在File中分配一个id,因为我不能用@Id对它进行注释,同时保持它可嵌入?目前,File表中的id为null。

注意

@Entity
public class Note {
    @Id
    @GeneratedValue
    @JsonProperty(value = "noteID", access = Access.READ_ONLY)
    private Long id;

    @ElementCollection
    @CollectionTable(
          name = "FILE",
          joinColumns = @JoinColumn(name = "NOTE_ID")
    )
    List<File> files;

}

文件

@Embeddable
public class File {
    private Long id;

    private String name;

    private String contentType;

    private String uri;
}
hibernate spring-data spring-data-jpa
3个回答
1
投票

只是让它成为一个合适的实体。 @Embeddable对值对象很有用,即由它们的值标识的对象。但是因为你需要一个id,这在你的场景中显然不是这样。

你的理由

根据我们的要求,文件仅存在于注释上下文中

选择嵌入是无效的。虽然我在互联网上看到过这样的短语,但你可以在任何你想要的环境中使用@Embeddables。作为@Embeddable,你的File实例不会或多或少地绑定到你的Note实例而不是@Entity


0
投票

当您需要不同的对象但单个实体时,请使用@Embeddableonly。即两个不同的类一起构成一个数据库表。


0
投票

您可以在org.hibernate.annotations包中使用@CollectionId注释。由于您无法根据上述答案将其设为实体,因此您可以使用@CollectionId注释。此批注需要3个属性:列,生成器和Collection对象中主键的类型。以下是一个例子来帮助您: -

import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;

 import javax.persistence.AttributeOverride;
 import javax.persistence.AttributeOverrides;
 import javax.persistence.Embedded;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.JoinTable;
 import javax.persistence.Lob;
 import javax.persistence.Table;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;

 import org.hibernate.annotations.CollectionId;
 import org.hibernate.annotations.GenericGenerator;
 import org.hibernate.annotations.Type;

 import javax.persistence.Column;
 import javax.persistence.ElementCollection;


 @Entity
 @Table(name="USER_DETAILS")
 public class UserDetails 
 {

   private int userId;

   private String  userName;
   private Date joinedDate;
   private String description;

   private Collection<Address> listOfAddresses=new ArrayList<Address>();
   @Id@GeneratedValue(strategy=GenerationType.SEQUENCE)
   public int getUserId() 
   {
    return userId;
   }
   public void setUserId(int userId) 
   {
    this.userId = userId;
   }
   public String getUserName() 
   {
    return userName;
   }
   public void setUserName(String userName) 
   {
    this.userName = userName;
   }
   @Temporal(TemporalType.DATE)
   public Date getJoinedDate() 
   {
    return joinedDate;
   }
   public void setJoinedDate(Date joinedDate) 
   {
    this.joinedDate = joinedDate;
   }
   @Lob
   public String getDescription() 
   {
    return description;
   }
   public void setDescription(String description) 
   {
    this.description = description;
   }
   public String toString()
   {
      return "[ "+getUserId()+", "+getUserName()+", "+", "+getJoinedDate()+", "+getDescription()+" ]";
   }
   @ElementCollection  
   @JoinTable(name="User_Address", joinColumns=@JoinColumn(name="User_Id"))
   @GenericGenerator(name="increment-gen",strategy="increment")
   @CollectionId(columns= {@Column(name="ADDRESS_ID")}, generator="increment-gen", type=@Type(type="long"))
   public Collection<Address> getListOfAddresses() {
    return listOfAddresses;
   }
   public void setListOfAddresses(Collection<Address> listOfAddresses) {
    this.listOfAddresses = listOfAddresses;
   }
 }

查看Collection的getter,我已经在@CollectionId中添加了注释,并且还指定了来自同一个包的@GenericGenerator,并提供了在集合中生成主键的策略。我在@CollectionId中使用了相同的Generator。希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.