我已经研究过如何解决这个问题,但没有一个对我有用。我知道我不完全理解它是如何工作的 - 这正是重点,但有时我必须停下来寻求帮助/澄清。
我正在从事一个侧面项目(并且正在研究我的编程技巧)。请注意,我是初中级别。
我正在尝试创建两个类,其中一个类具有多个一对一关系的字段。只有两个字段具有1到M的关系和M到M的关系。 “Dog”类有一个名为picture的字段,它与“Picture”类是1到M的关系。对于我的生活,每次我执行代码 - 它都会遇到问题。我已经尝试了几种方法使其工作 - 例如使类可序列化,使一个字段由另一个映射,反之亦然,使用CascadeType等等。我已经意识到也许我不太明白它在Java中是如何工作的(我正在使用IntelliJ),尽管我理解它的概念。
我读过并尝试过的文章仅举几例:
Entity Framework one-to-many and many-to-many relationship
Multiple 1:many relationship in Hibernate
http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-association
Hibernate One to Many and Many to One Relation
我有一种感觉,我真的很接近我想做的事情......但我似乎忽略了一些事情,非常。
我的数据库的意图:
我正在Dog表中的每一行都有自己的名字,重量等。 每行(狗)将分配许多图片。它们将以数据库中的URL地址的形式出现,我打算使用spring框架调用它。我找到了Cloudinary,并认为我可以尝试用我的代码来实现它们。我的这个侧面项目是改善现有的,工作实验室救援组织,它拥有我见过的最基本的网站。
以下是我的代码:
狗桌/班级
package com.claudiazeledon.LuckyLabsRescue.models;
import org.hibernate.annotations.Target;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
@Entity
public class Dog{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String name;
@Column
private Integer weight;
@Column
private Integer age;
@Column
private Gender gender;
@Column
private String color;
@OneToOne
private Temperament temperament;
@OneToOne
private Availability availability;
@OneToOne
private Health health;
@OneToMany
@JoinColumn(name = "picture_dp")
private Set<Picture> getPictures;
public Dog() {
}
public Dog(int id, String name, Integer weight, Integer age, Gender gender, String color, Temperament temperament, Availability availability, Health health, Set<Picture> getPictures) {
this.id = id;
this.name = name;
this.weight = weight;
this.age = age;
this.gender = gender;
this.color = color;
this.temperament = temperament;
this.availability = availability;
this.health = health;
this.getPictures = getPictures;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Temperament getTemperament() {
return temperament;
}
public void setTemperament(Temperament temperament) {
this.temperament = temperament;
}
public Availability getAvailability() {
return availability;
}
public void setAvailability(Availability availability) {
this.availability = availability;
}
public Health getHealth() {
return health;
}
public void setHealth(Health health) {
this.health = health;
}
public Set<Picture> getGetPictures() {
return getPictures;
}
public void setGetPictures(Set<Picture> getPictures) {
this.getPictures = getPictures;
}
}
图片表/类
package com.claudiazeledon.LuckyLabsRescue.models;
import javax.persistence.*;
import java.net.URL;
@Entity
@Table(name = "picture")
public class Picture{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "picture_dp", insertable = false, updatable = false)
private URL pictureURL;
// @ManyToOne
// @JoinColumn(name = "pictureId")
// @MapsId("pictureId")
// private Dog dogImage;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public URL getPictureURL() {
return pictureURL;
}
public void setPictureURL(URL pictureURL) {
this.pictureURL = pictureURL;
}
}
IntelliJ的错误消息
2017-11-16 15:20:35.300 INFO 24180 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate Core {5.0.12.Final}
2017-11-16 15:20:35.301 INFO 24180 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-11-16 15:20:35.302 INFO 24180 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-11-16 15:20:35.418 INFO 24180 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-11-16 15:20:35.518 INFO 24180 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2017-11-16 15:20:35.623 INFO 24180 --- [ restartedMain] o.h.e.j.e.i.LobCreatorBuilderImpl : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
2017-11-16 15:20:35.625 INFO 24180 --- [ restartedMain] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@673c77b3
2017-11-16 15:20:35.766 WARN 24180 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.claudiazeledon.LuckyLabsRescue.models.Picture.pictureURL references an unknown entity: java.net.URL
2017-11-16 15:20:35.771 INFO 24180 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2017-11-16 15:20:35.782 INFO 24180 --- [ restartedMain] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-11-16 15:20:35.792 ERROR 24180 --- [ restartedMain] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.claudiazeledon.LuckyLabsRescue.models.Picture.pictureURL references an unknown entity: java.net.URL
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at com.claudiazeledon.LuckyLabsRescue.LuckyLabsRescueApplication.main(LuckyLabsRescueApplication.java:10) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.8.RELEASE.jar:1.5.8.RELEASE]
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.claudiazeledon.LuckyLabsRescue.models.Picture.pictureURL references an unknown entity: java.net.URL
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:97) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processEndOfQueue(InFlightMetadataCollectorImpl.java:1786) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1730) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1617) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
... 21 common frames omitted
答案 0 :(得分:0)
您在-Swift.h
上建立了@ManyToOne
关系:
pictureUrl
实体之间建立了关系,但URL不是实体。
最简单的解决方案是删除该字段上的@ManyToOne
@JoinColumn(name = "picture_dp", insertable = false, updatable = false)
private URL pictureURL;
和@ManyToOne
注释,并使用常规@JoinColumn
注释替换它们。
@Column
答案 1 :(得分:0)
只看这个例子。您对一对多关系的疑问将显而易见。例如拿2个实体。 CustomerEntity.java
和ProductEntity.java
。在CustomerEntity.java
中添加
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "customer")
private Product product;
并在ProductEntity.java
中添加字段。
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "customer_id", nullable = false)
@JsonIgnore
private Customer customer;
implements Serializable
接口进行 getter setter设置。如果您使用的是json数据类型,则可以添加@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })