Java 无法确定以下列的类型:com.packt.cardatabase.domain.Owner,位于表:car:[org.hibernate.mapping.Column(Owner)]
我正在通过一本书学习Spring和Hibernate,还有一个可选的练习,通过使用Hibernate创建多对多关系。不幸的是,作者没有在GitHub上提供可选的示例,我在调试一些我不熟悉的东西时迷失了方向 下面是代码的部分,它作为车主和汽车表之间的一对多关系工作,但由于标题中的上述错误而失败。虽然涉及到一些文件,但我发布了Eclipse文件结构的图片 CardDatabase/pom文件:Java 无法确定以下列的类型:com.packt.cardatabase.domain.Owner,位于表:car:[org.hibernate.mapping.Column(Owner)],java,spring,hibernate,spring-boot,spring-data-jpa,Java,Spring,Hibernate,Spring Boot,Spring Data Jpa,我正在通过一本书学习Spring和Hibernate,还有一个可选的练习,通过使用Hibernate创建多对多关系。不幸的是,作者没有在GitHub上提供可选的示例,我在调试一些我不熟悉的东西时迷失了方向 下面是代码的部分,它作为车主和汽车表之间的一对多关系工作,但由于标题中的上述错误而失败。虽然涉及到一些文件,但我发布了Eclipse文件结构的图片 CardDatabase/pom文件: <?xml version="1.0" encoding="UTF-8"?> <proj
<?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>
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;
}
}
package com.packt.cardatabase.domain;
导入java.util.Set//导入以能够使用Set方法。
//注意,这依赖于pom.xml文件中持久性包的依赖关系
//该依赖项必须存在,类才能看到这一点。
导入javax.persistence.*//这包括下面使用的Id、GeneratedValue和GeneratedType类。
@实体
公车
{
@Id//主键是使用@Id注释定义的
@GeneratedValue(strategy=GenerationType.AUTO)//定义ID#由数据库自动生成
私人长id;
私有字符串品牌、型号、颜色、注册号;
私人年费,价格;
私人业主;
/*
//以下两行定义了从汽车到车主的多对一关系
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“owner”)
*/
//如果要使用其他命名约定,请使用@Column注释
//这将允许您定义列的长度以及该列是否可为空
/*例如:
@列(name=“desc”,null=false,长度=512)
私有字符串描述;
*/
公共汽车(字符串品牌、字符串型号、字符串颜色、字符串注册编号、整数年份、整数价格、车主)
{
//这是一个自动生成的构造函数
超级();
这个品牌=品牌;
this.model=模型;
这个颜色=颜色;
this.registerNumber=registerNumber;
今年=年;
这个价格=价格;
this.owner=所有者;
}
//以下四行在cars/owners表中创建多对多关系
@许多(mappedBy=“cars”)
私人业主;
公共集getOwners(){return owners;}
public void setOwners(Set owners){this.owners=owners;}
公共所有者getOwner(){
归还所有人;
}
公共无效集合所有者(所有者){
this.owner=所有者;
}
//以下是所有自动生成的getter和setter
公共字符串getBrand(){
回归品牌;
}
公共品牌(字符串品牌){
这个品牌=品牌;
}
公共字符串getModel(){
收益模型;
}
公共void集合模型(字符串模型){
this.model=模型;
}
公共字符串getColor(){
返回颜色;
}
公共void setColor(字符串颜色){
这个颜色=颜色;
}
公共字符串getRegisterNumber(){
返回注册表编号;
}
public void setRegisterNumber(字符串registerNumber){
this.registerNumber=registerNumber;
}
公共int getYear(){
回归年;
}
公共年(国际年){
今年=年;
}
public int getPrice(){
退货价格;
}
公共无效设置价格(整数价格){
这个价格=价格;
}
}
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);
}
package com.packt.cardatabase.domain;
import org.springframework.data.repository.CrudRepository;
public interface OwnerRepository extends CrudRepository<Owner, Long>
{}
package com.packt.cardatabase.domain;
导入java.util.List//允许使用列表关键字
导入org.springframework.data.jpa.repository.Query//允许使用查询注释
导入org.springframework.data.repository.crudepository;
公共接口存储扩展了crudepository
{
//以下是所有自定义查询:
//按颜色取车
列表findByColor(字符串颜色);
//按年取车
列表查找年份(整数年);
//按品牌和型号取车
列出FindBerandModel(字符串品牌、字符串模型);
//使用@Query注释,使用SQL按品牌获取汽车。
//记住在上面的导入中包括查询类。
@查询(“从c车中选择c,其中c.brand=?1”)
列出findByBrand(字符串);
}
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;
}
}
package com.packt.cardatabase.domain;
导入javax.persistence.*;
导入java.util.*;
@实体
公共类所有者
{
@Id//主键是使用@Id注释定义的
@GeneratedValue(strategy=GenerationType.AUTO)//定义由数据库自动生成ID
私人长期所有权;
私有字符串firstname,lastname;
//下一行在车主和汽车之间创建一对多关系
//“级联”属性意味着,如果删除所有者,所有链接的车辆也将被删除。
//mappedBy=“owner”属性意味着car类具有owner字段,该字段是关系的外键。
//@OneToMany(cascade=CascadeType.ALL,mappedBy=“owner”)
//私家车名单;
公共所有者(){}
//以下内容是使用source->Generate Constructor using fields自动生成的
/**
*@param ownerid
*@param firstname
*@param lastname
*/
公共所有者(字符串名、字符串名)
{
超级();
this.firstname=firstname;
this.lastname=lastname;
}
//接下来的3行创建了一个多对多关系,并将“id”和“owner\u id”列连接到一个名为“car\u owner”的新表中。
@多个(级联=级联类型.MERGE)
@JoinTable(name=“car\u owner”,joinColumns={@JoinColumn(name=“ownerid”)},inverseJoinColumns={@JoinColumn(name=“id”)})
私家车组=新哈希集(0);
公共设置getCars(){return cars;}
公共无效设置车辆(设置车辆){this.cars=cars;}
/*
公共列表getCars(){
返回车辆;
}
公共车辆(列出车辆){
这个。汽车=汽车;
}
*/
//下面是自动生成的带有注释的getter和setter
/**
*@返回所有者ID
*/
公共长getOwnerid(){
返回所有者ID;
}
/**
*@param ownerid要设置的ownerid
*/
public void setOwnerid(长所有者)
package com.packt.cardatabase.domain;
import org.springframework.data.repository.CrudRepository;
public interface OwnerRepository extends CrudRepository<Owner, Long>
{}
private Owner owner;
@Entity
public class CarOwner implements Serializable {
@EmbeddedId
private CarOwneId id;
@ManyToOne
@JoinColumn(name = "car_id", referencedColumnName = "car_id", insertable = false, updatable = false)
private Car car;
@ManyToOne
@JoinColumn(name = "owner_id", referencedColumnName = "owner_id", insertable = false, updatable = false)
private Owner owner;
public CarOwner(Car c, Owner o) {
// create primary key
this.id = new CarOwneId(c.getId(), o.getOwnerid());
// initialize attributes
this.car = c;
this.owner = o;
}
public CarOwneId getId() {
return id;
}
public void setId(CarOwneId id) {
this.id = id;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public Owner getOwner() {
return owner;
}
public void setOwner(Owner owner) {
this.owner = owner;
}
@Embeddable
public static class CarOwneId implements Serializable {
@Column(name = "car_id")
protected Long carId;
@Column(name = "owner_id")
protected Long ownerId;
public CarOwneId() {
}
public CarOwneId(Long carId, Long ownerId) {
this.carId = carId;
this.ownerId = ownerId;
}
public Long getCarId() {
return carId;
}
public void setCarId(Long carId) {
this.carId = carId;
}
public Long getOwnerId() {
return ownerId;
}
public void setOwnerId(Long ownerId) {
this.ownerId = ownerId;
}
}
}