无法使用 Spring-Boot & Spring-JPA 保存 "Many"-@OneToMany 的实体
Can't save "Many"-Entity of @OneToMany with Spring-Boot & Spring-JPA
我正在使用 Spring 引导和 MySQL。我按照 this link 设置了所有内容,并且能够连接到 MySql 和 read/write 数据。但是存在 1:n 关系,我无法保存多方实体:
@Entity
public class OneSideOfRelationship {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long oneId;
private String someText;
@OneToMany(mappedBy="oneId")
private List<ManySideOfRelationship> manySide;
[Constructor / Getter / Setter]
}
@Entity
public class ManySideOfRelationship {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long manyId;
@ManyToOne(targetEntity=OneSideOfRelationship.class)
@JoinColumn
private long oneId;
private String someMoreText;
[Constructor / Getter / Setter]
}
@Transactional
public interface OneDao extends CrudRepository<OneSideOfRelationship, Long> {}
@Transactional
public interface ManyDao extends CrudRepository<ManySideOfRelationship, Long> {}
如果我在我的控制器中这样做:
[...]
@Autowired
@private ManySideOfRelationship manyDao;
[...]
ManySideOfRelationship many = new ManySideOfRelationship();
many.setOneId(1L);
many.setSomeMoreText("Text");
manyDao.save(many);
[...]
我得到了:
org.springframework.orm.jpa.JpaSystemException: could not get a field value by reflection getter of com.package.database.OneSideOfRelationship.oneId; nested exception is org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.package.database.OneSideOfRelationship.oneId
出于我的 application.properties:
spring.datasource.url = jdbc:mysql://myurl:myport/mydatabase
spring.datasource.username = myusername
spring.datasource.password = mypassword
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
还有一些来自 pom.xml
的东西
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
希望有人有想法。
oneId
必须是 OneSideOfRelationship
的数据类型,不长。
@ManyToOne(targetEntity=OneSideOfRelationship.class)
@JoinColumn
private OneSideOfRelationship oneId;
我正在使用 Spring 引导和 MySQL。我按照 this link 设置了所有内容,并且能够连接到 MySql 和 read/write 数据。但是存在 1:n 关系,我无法保存多方实体:
@Entity
public class OneSideOfRelationship {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long oneId;
private String someText;
@OneToMany(mappedBy="oneId")
private List<ManySideOfRelationship> manySide;
[Constructor / Getter / Setter]
}
@Entity
public class ManySideOfRelationship {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long manyId;
@ManyToOne(targetEntity=OneSideOfRelationship.class)
@JoinColumn
private long oneId;
private String someMoreText;
[Constructor / Getter / Setter]
}
@Transactional
public interface OneDao extends CrudRepository<OneSideOfRelationship, Long> {}
@Transactional
public interface ManyDao extends CrudRepository<ManySideOfRelationship, Long> {}
如果我在我的控制器中这样做:
[...]
@Autowired
@private ManySideOfRelationship manyDao;
[...]
ManySideOfRelationship many = new ManySideOfRelationship();
many.setOneId(1L);
many.setSomeMoreText("Text");
manyDao.save(many);
[...]
我得到了:
org.springframework.orm.jpa.JpaSystemException: could not get a field value by reflection getter of com.package.database.OneSideOfRelationship.oneId; nested exception is org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.package.database.OneSideOfRelationship.oneId
出于我的 application.properties:
spring.datasource.url = jdbc:mysql://myurl:myport/mydatabase
spring.datasource.username = myusername
spring.datasource.password = mypassword
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
还有一些来自 pom.xml
的东西 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
希望有人有想法。
oneId
必须是 OneSideOfRelationship
的数据类型,不长。
@ManyToOne(targetEntity=OneSideOfRelationship.class)
@JoinColumn
private OneSideOfRelationship oneId;