- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在通过一本书学习 Spring 和 Hibernate,并且有一个可选练习来使用 Hibernate 创建多对多关系。不幸的是,作者没有在 GitHub 上提供可选示例,我在调试一些我不熟悉的东西时迷失了方向。
这是代码的一部分,它作为 Owner 和 Car 表之间的一对多关系工作,但作为多对多关系失败,标题中出现上述错误。涉及到一些文件,但我发布了 Eclipse 文件结构的图片。
卡数据库/pom 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- Maven uses this POM file to determine dependencies -->
<groupId>com.packt</groupId>
<artifactId>cardatabase</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cardatabase</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version> 2.0.4.RELEASE </version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-data-jpa</artifactId>
<version> 2.0.4.RELEASE</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<!-- This is the dependency for the MariaDB program -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
应用程序属性:
logging.level.root = INFO
spring.jpa.show-sql=true
spring.datasource.url=jdbc:mariadb://localhost:3306/cardb
spring.datasource.username=root
spring.datasource.password=nesatis
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.generate-ddl=true
spring.jpa.generate.ddl-auto=create-drop
CardatabaseApplication.java
package com.packt.cardatabase;
import org.springframework.beans.factory.annotation.Autowired;//This enables dependency injection
//These next four lines are for the commandlinerunner which allows code to run before the application has fully started.
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.packt.cardatabase.domain.Car;
import com.packt.cardatabase.domain.CarRepository;
import com.packt.cardatabase.domain.Owner;
import com.packt.cardatabase.domain.OwnerRepository;
@SpringBootApplication //Enables spring boot automatic configuration
public class CardatabaseApplication
{
@Autowired //This is used to enable dependency injection
private CarRepository repository;
@Autowired //Inject ownerrepository into the main class
private OwnerRepository orepository;
public static void main(String[] args)
{
//After adding this comment the application is restarted.
SpringApplication.run(CardatabaseApplication.class, args);
}
@Bean
CommandLineRunner runner() {
return args ->{
Owner owner1 = new Owner("John", "Johnson");
Owner owner2 = new Owner("Mary", "Johnson");
orepository.save(owner1);
orepository.save(owner2);
// Enter Car data here. This data must fit the Car constructor String X4 int X2
// Methods such as save are a part of the CRUD
repository.save(new Car("Ford", "Mustang", "Red" , "ADF-1121", 2017, 59000, owner1));
repository.save(new Car("Nissan", "Leaf", "White", "SSJ-3002", 2014, 29000, owner2));
repository.save(new Car("Toyota", "Prius", "Silver", "KKO-0212", 2018, 39000, owner2));
repository.save(new Car("Honda", "Accord", "White", "AH46505", 2014, 25000, owner1));
};
}
}
汽车.java
package com.packt.cardatabase.domain;
import java.util.Set; //Imported to be able to use the Set method.
//Note this relies on the dependency for the persistence package in the pom.xml file
//That dependency must be there in order for the class to see this.
import javax.persistence.*; //This includes the Id, GeneratedValue and GeneratedType class used below.
@Entity
public class Car
{
@Id //The primary key is defined by using the @Id annotation
@GeneratedValue(strategy=GenerationType.AUTO) //defines that the ID # is automatically generated by the database
private long id;
private String brand, model, color, registerNumber;
private int year, price;
private Owner owner;
/*
//The following two lines define a many to one relationship from car to owner
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "owner")
*/
// If you want to use some other naming convention, use the @Column annotation
// This will let you also define the columns length and whether the column is nullable
/* example:
@Column(name="desc", nullable=false, length=512)
private String description;
*/
public Car(String brand, String model, String color, String registerNumber, int year, int price, Owner owner)
{
//This is an auto-genreated constructor
super();
this.brand = brand;
this.model = model;
this.color = color;
this.registerNumber = registerNumber;
this.year = year;
this.price = price;
this.owner = owner;
}
//The following four lines create a many to many relationship in the cars/owners tables
@ManyToMany(mappedBy = "cars")
private Set<Owner> owners;
public Set<Owner> getOwners(){return owners;}
public void setOwners(Set<Owner> owners) {this.owners = owners;}
public Owner getOwner() {
return owner;
}
public void setOwner(Owner owner) {
this.owner = owner;
}
//The following are all auto-genreated getters and setters
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getRegisterNumber() {
return registerNumber;
}
public void setRegisterNumber(String registerNumber) {
this.registerNumber = registerNumber;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
CarRepository.java
package com.packt.cardatabase.domain;
import java.util.List; //allows the list keyword
import org.springframework.data.jpa.repository.Query;//Allows the use of the Query annotation
import org.springframework.data.repository.CrudRepository;
public interface CarRepository extends CrudRepository<Car, Long>
{
//The following are all custom queries:
//Fetch Cars by color
List<Car> findByColor(String color);
//Fetch Cars by year
List<Car> findByYear(int year);
//Fetch Cars by brand and model
List<Car> findByBrandAndModel(String Brand, String Model);
//Fetch cars by brand using SQL using the @Query annotation.
//Remember to include the Query class in the imports above.
@Query("Select c from Car c where c.brand = ?1")
List<Car> findByBrand(String bran);
}
Owner.java
package com.packt.cardatabase.domain;
import javax.persistence.*;
import java.util.*;
@Entity
public class Owner
{
@Id //The primary key is defined by using the @Id annotation
@GeneratedValue(strategy=GenerationType.AUTO) //defines that theID is automatically generated by the database
private long ownerid;
private String firstname, lastname;
//The following line creates a One to many relationship between Owner and Car
//The cascade attribute means that if the owner is deleted, all linked cars are deleted too.
//The mappedBy="owner" attribute means that the car class has the owner field which is the foreign key for the relationship.
//@OneToMany(cascade = CascadeType.ALL, mappedBy="owner")
//private List<Car> cars;
public Owner() {}
//The following was auto generated using source -> Generate Constructor using fields
/**
* @param ownerid
* @param firstname
* @param lastname
*/
public Owner(String firstname, String lastname)
{
super();
this.firstname = firstname;
this.lastname = lastname;
}
//The next 3 lines create a many to many relationship and join the columns 'id' and 'owner_id' to a new table called 'car_owner'.
@ManyToMany(cascade = CascadeType.MERGE)
@JoinTable(name = "car_owner", joinColumns = {@JoinColumn(name = "ownerid")}, inverseJoinColumns = {@JoinColumn(name = "id")})
private Set<Car> cars = new HashSet<Car>(0);
public Set<Car> getCars(){return cars;}
public void setCars(Set<Car> cars) {this.cars = cars;}
/*
public List<Car> getCars(){
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
*/
//The following are auto-generated getters and setters with comments
/**
* @return the ownerid
*/
public long getOwnerid() {
return ownerid;
}
/**
* @param ownerid the ownerid to set
*/
public void setOwnerid(long ownerid) {
this.ownerid = ownerid;
}
/**
* @return the firstname
*/
public String getFirstname() {
return firstname;
}
/**
* @param firstname the firstname to set
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}
/**
* @return the lastname
*/
public String getLastname() {
return lastname;
}
/**
* @param lastname the lastname to set
*/
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
OwnerRepository.java
package com.packt.cardatabase.domain;
import org.springframework.data.repository.CrudRepository;
public interface OwnerRepository extends CrudRepository<Owner, Long>
{}
Eclipse 中文件的结构:
最佳答案
删除 Owner 字段,将其标记为 @Transient,或者与其建立 ManyToOne/OneToMany/OneToOne 关系。那是你的问题。
关于java - 无法确定 : com. packt.cardatabase.domain.Owner 的类型,位于表 : car, 的列 : [org. hibernate.mapping.Column(owner)],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52350118/
我有一台 MySQL 服务器和一台 PostgreSQL 服务器。 需要从多个表中复制或重新插入一组数据 MySQL 流式传输/同步到 PostgreSQL 表。 这种复制可以基于时间(Sync)或事
如果两个表的 id 彼此相等,我尝试从一个表中获取数据。这是我使用的代码: SELECT id_to , email_to , name_to , status_to
我有一个 Excel 工作表。顶行对应于列名称,而连续的行每行代表一个条目。 如何将此 Excel 工作表转换为 SQL 表? 我使用的是 SQL Server 2005。 最佳答案 这取决于您使用哪
我想合并两个 Django 模型并创建一个模型。让我们假设我有第一个表表 A,其中包含一些列和数据。 Table A -------------- col1 col2 col3 col
我有两个表:table1,table2,如下所示 table1: id name 1 tamil 2 english 3 maths 4 science table2: p
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 1 年前。 Improve th
下面两个语句有什么区别? newTable = orginalTable 或 newTable.data(originalTable) 我怀疑 .data() 方法具有性能优势,因为它在标准 AX 中
我有一个表,我没有在其中显式定义主键,它并不是真正需要的功能......但是一位同事建议我添加一个列作为唯一主键以随着数据库的增长提高性能...... 谁能解释一下这是如何提高性能的? 没有使用索引(
如何将表“产品”中的产品记录与其不同表“图像”中的图像相关联? 我正在对产品 ID 使用自动增量。 我觉得不可能进行关联,因为产品 ID 是自动递增的,因此在插入期间不可用! 如何插入新产品,获取产品
我有一个 sql 表,其中包含关键字和出现次数,如下所示(尽管出现次数并不重要): ____________ dog | 3 | ____________ rat | 7 | ____
是否可以使用目标表中的LAST_INSERT_ID更新源表? INSERT INTO `target` SELECT `a`, `b` FROM `source` 目标表有一个自动增量键id,我想将其
我正在重建一个搜索查询,因为它在“我看到的”中变得多余,我想知道什么 (albums_artists, artists) ( ) does in join? is it for boosting pe
以下是我使用 mysqldump 备份数据库的开关: /usr/bin/mysqldump -u **** --password=**** --single-transaction --databas
我试图获取 MySQL 表中的所有行并将它们放入 HTML 表中: Exam ID Status Assigned Examiner
如何查询名为 photos 的表中的所有记录,并知道当前用户使用单个查询将哪些结果照片添加为书签? 这是我的表格: -- -- Table structure for table `photos` -
我的网站都在 InnoDB 表上运行,目前为止运行良好。现在我想知道在我的网站上实时发生了什么,所以我将每个页面浏览量(页面、引荐来源网址、IP、主机名等)存储在 InnoDB 表中。每秒大约有 10
我在想我会为 mysql 准备两个表。一个用于存储登录信息,另一个用于存储送货地址。这是传统方式还是所有内容都存储在一张表中? 对于两个表...有没有办法自动将表 A 的列复制到表 B,以便我可以引用
我不是程序员,我从这个表格中阅读了很多关于如何解决我的问题的内容,但我的搜索效果不好 我有两张 table 表 1:成员 id*| name | surname -------------------
我知道如何在 ASP.NET 中显示真实表,例如 public ActionResult Index() { var s = db.StaffInfoDBSet.ToList(); r
我正在尝试运行以下查询: "insert into visits set source = 'http://google.com' and country = 'en' and ref = '1234
我是一名优秀的程序员,十分优秀!