我正在尝试创建一个@OneToMany关系,虽然我在这里阅读了几个答案但我仍然得到了PSQLException。
我正在尝试创建包含许多Media对象的Track。所以我跟着this回答。
问题:
在尝试保存包含多个Track
的A Medias
时:
PSQLException: ERROR: insert or update on table medias violates foreign key constraint
Detail: Key (normmediatitle)=(SOME_MEDIA_TITLE) is not present in table tracks.
我缺少什么?
媒体:
@Entity(name = "medias")
public class Media {
static Logger logger = LoggerFactory.getLogger(Media.class);
@Id
@NotNull
@Column(name="normmediatitle")
private String title;
@GeneratedValue(strategy = GenerationType.AUTO, generator = "media_seq_gen")
@SequenceGenerator(name = "media_seq_gen", sequenceName = "mediaSeq")
private long id;
@NotNull
@Column(unique = true)
private String videoId;
:跟踪:
@Entity(name = "tracks")
public class Track {
static Logger logger = LoggerFactory.getLogger(Track.class);
@Id
@NotNull
@Column(name="normtracktitle")
private String title;
@NotNull
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="normmediatitle", referencedColumnName="normtracktitle")
private List<Media> medias;
更新 - Pom.xml:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>
答案 0 :(得分:0)
我最终按照JPA 1.0标准执行了@OneToMany。这不是单向的,但对我来说更有意义:)。
我发现this网站可靠,并且已经解释了JPA 1.0和JPA 2.0实现。