Spring boot 不要让我创建一个没有数据库的仓库
Spring boot don't let me create a repository without database
我已经在 Spring Boot 上创建了一个项目。
我有两个提供程序扩展了同一个抽象提供程序,我在启动时通过 Spring 配置文件加载了我感兴趣的那个。
其中一个提供商基于 JPA,另一个在我调用 Web 服务的地方实现了他的接口。
这是我不想使用数据库的提供程序的界面:
package net.worldline.mst.metro.ds.core.massilia.provider;
import net.worldline.mst.metro.ds.core.contract.IProductRepository;
import net.worldline.mst.metro.ds.core.massilia.model.MassiliaProduct;
import org.springframework.context.annotation.Profile;
import org.springframework.data.repository.NoRepositoryBean;
@Profile("massilia")
@NoRepositoryBean
public interface MassiliaProductRepository extends IProductRepository<MassiliaProduct,String> {
}
这是提供者使用数据库的界面:
package net.worldline.mst.metro.ds.core.local.provider;
import net.worldline.mst.metro.ds.core.contract.IProductRepository;
import net.worldline.mst.metro.ds.core.local.model.Product;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
import org.springframework.stereotype.Repository;
@Profile("local")
@Repository
public interface MonBoProductRepository extends IProductRepository<Product,String> {
@Query("select p.variants from Product p where p.ean = :ean")
List<Product> findVariantByEan(@Param("ean") String ean);
@Query("select p.companions from Product p where p.ean = :ean")
List<Product> findCompanionByEan(@Param("ean") String ean);
}
他们共同扩展了这个接口:
package net.worldline.mst.metro.ds.core.contract;
import net.worldline.mst.metro.ds.core.model.AbstractProduct;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.web.bind.annotation.PathVariable;
import java.io.Serializable;
import java.util.List;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.PathVariable;
import java.io.Serializable;
import java.util.List;
@NoRepositoryBean
public interface IProductRepository<T extends AbstractProduct,ID extends Serializable> extends CrudRepository<T, ID> {
@RestResource(path = "byEAN")
T findByEan(@Param("ref") Integer ean);
T findProductByEan(@PathVariable ID ean);
List<T> findVariantByEan(@PathVariable ID ean);
List<T> findCompanionByEan(@PathVariable ID ean);
}
不使用数据库的提供商有一个实现,出于工作原因,我无法向您展示该实现,但它会调用内部网络服务
像我的提供者一样,我有两个模型,扩展相同的抽象class。
一个用@Entity、@Id 和 co 注释,我不想在另一个上添加这个注释 class,因为对我来说,我已经明确表示我不想通过在 application-${profile}.properties
.
中询问 none 任何数据库
这是我在 bdd 中使用的模型:
package net.worldline.mst.metro.ds.core.local.model;
import net.worldline.mst.metro.ds.core.model.AbstractProduct;
import net.worldline.mst.metro.ds.core.model.AbstractProductCharacteristic;
import org.hibernate.validator.constraints.NotEmpty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name = "PRODUCTS")
@Profile("local")
public class Product extends AbstractProduct {
private static final Logger log = LoggerFactory.getLogger(Product.class);
@ManyToMany(
fetch = FetchType.LAZY
)
@JoinTable(
name="products_to_variants",
joinColumns = @JoinColumn(name="productEan"),
inverseJoinColumns = @JoinColumn(name="productEanVariant")
)
private List<Product> variants;
@ManyToMany(
fetch = FetchType.LAZY
)
@JoinTable(
name="products_to_companions",
joinColumns = @JoinColumn(name="productEan"),
inverseJoinColumns = @JoinColumn(name="productEanCompanion")
)
private List<Product> companions;
@Column(name = "accroche")
private String accroche;
@Id
@Column(name = "ean", unique = false)
private String ean;
@Column(name = "descriptif")
private String descriptif;
@Column(name = "libelle")
@NotEmpty
private String libelle;
@Column(name = "oldPrice")
private String oldPrice;
@Column(name = "price")
@NotEmpty
//@Digits(fraction = 0, integer = 10)
private String price;
@Column(name = "stock")
private String stock;
@OneToMany(mappedBy = "ean" )
protected List<ProductCharacteristic> characteristics;
@OneToMany(mappedBy = "product" )
@NotEmpty
protected List<ProductVisual> visuals;
public List<Product> getVariants() {
return variants;
}
public void setVariants(List<Product> variants) {
this.variants = variants;
}
public List<Product> getCompanions() {
return companions;
}
public void setCompanions(List<Product> companions) {
this.companions = companions;
}
@Override
public String getAccroche() {
return accroche;
}
@Override
public void setAccroche(String accroche) {
this.accroche = accroche;
}
@Override
public String getEan() {
return ean;
}
public void setRef(String ean) {
this.ean = ean;
}
@Override
public String getLibelle() {
return libelle;
}
@Override
public void setLibelle(String libelle) {
this.libelle = libelle;
}
@Override
public String getOldPrice() {
return oldPrice;
}
@Override
public void setOldPrice(String oldPrice) {
this.oldPrice = oldPrice;
}
@Override
public String getPrice() {
return price;
}
@Override
public void setPrice(String price) {
this.price = price;
}
@Override
public String getStock() {
return stock;
}
@Override
public void setStock(String stock) {
this.stock = stock;
}
@Override
public List<? extends AbstractProductCharacteristic> getCharacteristics() {
return characteristics;
}
@Override
public List<ProductVisual> getVisuals() {
return visuals;
}
public String getDescriptif() {
return this.descriptif;
}
public void setDescriptif(String descriptif) {
this.descriptif=descriptif;
}
}
这是我不想与数据库一起使用的模型:
package net.worldline.mst.metro.ds.core.massilia.model;
import net.worldline.mst.metro.ds.core.model.AbstractProduct;
import org.springframework.context.annotation.Profile;
import javax.persistence.*;
import java.util.List;
@Profile("massilia")
public class MassiliaProduct extends AbstractProduct {
@Override
public String getEan() { return this.ean; }
@Override
public String getLibelle() { return this.libelle; }
@Override
public String getPrice() { return this.price; }
@Override
public String getAccroche() { return this.accroche; }
@Override
public String getOldPrice() { return oldPrice; }
@Override
public String getStock() { return stock; }
@Override
public String getDescriptif() {
return descriptif;
}
@Override
public List<MassiliaCharacteristic> getCharacteristics() {
return (List<MassiliaCharacteristic>)characteristics;
}
@Override
public List<MassiliaProductVisual> getVisuals() {
return (List<MassiliaProductVisual>)visuals;
}
}
他们共享这个模型:
package net.worldline.mst.metro.ds.core.model;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.hateoas.core.Relation;
import java.util.List;
@Relation(value = "product", collectionRelation = "product")
public abstract class AbstractProduct extends ResourceSupport {
protected String ean;
protected String libelle;
protected String accroche;
protected String price;
protected String oldPrice;
protected String stock;
protected String descriptif;
protected List<? extends AbstractProductCharacteristic> characteristics;
protected List<? extends AbstractProductVisual> visuals;
public abstract String getEan();
public abstract String getLibelle();
public abstract String getPrice();
public abstract String getAccroche();
public abstract String getOldPrice();
public abstract String getStock();
public abstract List<? extends AbstractProductCharacteristic> getCharacteristics();
public abstract List<? extends AbstractProductVisual> getVisuals();
public abstract String getDescriptif();
public void setEan(String ean) {
this.ean = ean;
}
public void setLibelle(String libelle) {
this.libelle = libelle;
}
public void setPrice(String price) {
this.price = price;
}
public void setAccroche(String accroche) {
this.accroche = accroche;
}
public void setOldPrice(String oldPrice) {
this.oldPrice = oldPrice;
}
public void setStock(String stock) {
this.stock = stock;
}
public void setCharacteristics(List<? extends AbstractProductCharacteristic> characteristics) {
this.characteristics = characteristics;
}
public void setVisuals(List<? extends AbstractProductVisual> visuals) {
this.visuals = visuals;
}
public void setDescriptif(String descriptif) {
this.descriptif = descriptif;
}
}
在申请中-${profile}.properties
,我精确:
spring.datasource.platform = hsqldb for the jpa instance
.
spring.datasource.platform = none for the instance where i call my webservices.
我的问题很简单:我希望 spring 通过实现存储库让我做我想做的事,但是当我启动我的服务器时,spring 说我的对象不受管理,所以如果我不将 @Entity 添加到我的模型中,它就不会 运行.
那么为什么 Spring 数据看起来像是默认加载 JPA 存储库?
实际上是人为错误。
我在 application.properties
文件中忘记了一个 spring.datasource.platform = hsqldb
。
我没有看它因为我正在使用 spring 配置文件所以我正在查看我的 application-massilia.properties
至极包含 spring.datasource.platform = none
并且现在正在收听因为我已经删除另一个文件中的副本。
我已经在 Spring Boot 上创建了一个项目。
我有两个提供程序扩展了同一个抽象提供程序,我在启动时通过 Spring 配置文件加载了我感兴趣的那个。
其中一个提供商基于 JPA,另一个在我调用 Web 服务的地方实现了他的接口。
这是我不想使用数据库的提供程序的界面:
package net.worldline.mst.metro.ds.core.massilia.provider;
import net.worldline.mst.metro.ds.core.contract.IProductRepository;
import net.worldline.mst.metro.ds.core.massilia.model.MassiliaProduct;
import org.springframework.context.annotation.Profile;
import org.springframework.data.repository.NoRepositoryBean;
@Profile("massilia")
@NoRepositoryBean
public interface MassiliaProductRepository extends IProductRepository<MassiliaProduct,String> {
}
这是提供者使用数据库的界面:
package net.worldline.mst.metro.ds.core.local.provider;
import net.worldline.mst.metro.ds.core.contract.IProductRepository;
import net.worldline.mst.metro.ds.core.local.model.Product;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
import org.springframework.stereotype.Repository;
@Profile("local")
@Repository
public interface MonBoProductRepository extends IProductRepository<Product,String> {
@Query("select p.variants from Product p where p.ean = :ean")
List<Product> findVariantByEan(@Param("ean") String ean);
@Query("select p.companions from Product p where p.ean = :ean")
List<Product> findCompanionByEan(@Param("ean") String ean);
}
他们共同扩展了这个接口:
package net.worldline.mst.metro.ds.core.contract;
import net.worldline.mst.metro.ds.core.model.AbstractProduct;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.web.bind.annotation.PathVariable;
import java.io.Serializable;
import java.util.List;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.PathVariable;
import java.io.Serializable;
import java.util.List;
@NoRepositoryBean
public interface IProductRepository<T extends AbstractProduct,ID extends Serializable> extends CrudRepository<T, ID> {
@RestResource(path = "byEAN")
T findByEan(@Param("ref") Integer ean);
T findProductByEan(@PathVariable ID ean);
List<T> findVariantByEan(@PathVariable ID ean);
List<T> findCompanionByEan(@PathVariable ID ean);
}
不使用数据库的提供商有一个实现,出于工作原因,我无法向您展示该实现,但它会调用内部网络服务
像我的提供者一样,我有两个模型,扩展相同的抽象class。
一个用@Entity、@Id 和 co 注释,我不想在另一个上添加这个注释 class,因为对我来说,我已经明确表示我不想通过在 application-${profile}.properties
.
这是我在 bdd 中使用的模型:
package net.worldline.mst.metro.ds.core.local.model;
import net.worldline.mst.metro.ds.core.model.AbstractProduct;
import net.worldline.mst.metro.ds.core.model.AbstractProductCharacteristic;
import org.hibernate.validator.constraints.NotEmpty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name = "PRODUCTS")
@Profile("local")
public class Product extends AbstractProduct {
private static final Logger log = LoggerFactory.getLogger(Product.class);
@ManyToMany(
fetch = FetchType.LAZY
)
@JoinTable(
name="products_to_variants",
joinColumns = @JoinColumn(name="productEan"),
inverseJoinColumns = @JoinColumn(name="productEanVariant")
)
private List<Product> variants;
@ManyToMany(
fetch = FetchType.LAZY
)
@JoinTable(
name="products_to_companions",
joinColumns = @JoinColumn(name="productEan"),
inverseJoinColumns = @JoinColumn(name="productEanCompanion")
)
private List<Product> companions;
@Column(name = "accroche")
private String accroche;
@Id
@Column(name = "ean", unique = false)
private String ean;
@Column(name = "descriptif")
private String descriptif;
@Column(name = "libelle")
@NotEmpty
private String libelle;
@Column(name = "oldPrice")
private String oldPrice;
@Column(name = "price")
@NotEmpty
//@Digits(fraction = 0, integer = 10)
private String price;
@Column(name = "stock")
private String stock;
@OneToMany(mappedBy = "ean" )
protected List<ProductCharacteristic> characteristics;
@OneToMany(mappedBy = "product" )
@NotEmpty
protected List<ProductVisual> visuals;
public List<Product> getVariants() {
return variants;
}
public void setVariants(List<Product> variants) {
this.variants = variants;
}
public List<Product> getCompanions() {
return companions;
}
public void setCompanions(List<Product> companions) {
this.companions = companions;
}
@Override
public String getAccroche() {
return accroche;
}
@Override
public void setAccroche(String accroche) {
this.accroche = accroche;
}
@Override
public String getEan() {
return ean;
}
public void setRef(String ean) {
this.ean = ean;
}
@Override
public String getLibelle() {
return libelle;
}
@Override
public void setLibelle(String libelle) {
this.libelle = libelle;
}
@Override
public String getOldPrice() {
return oldPrice;
}
@Override
public void setOldPrice(String oldPrice) {
this.oldPrice = oldPrice;
}
@Override
public String getPrice() {
return price;
}
@Override
public void setPrice(String price) {
this.price = price;
}
@Override
public String getStock() {
return stock;
}
@Override
public void setStock(String stock) {
this.stock = stock;
}
@Override
public List<? extends AbstractProductCharacteristic> getCharacteristics() {
return characteristics;
}
@Override
public List<ProductVisual> getVisuals() {
return visuals;
}
public String getDescriptif() {
return this.descriptif;
}
public void setDescriptif(String descriptif) {
this.descriptif=descriptif;
}
}
这是我不想与数据库一起使用的模型:
package net.worldline.mst.metro.ds.core.massilia.model;
import net.worldline.mst.metro.ds.core.model.AbstractProduct;
import org.springframework.context.annotation.Profile;
import javax.persistence.*;
import java.util.List;
@Profile("massilia")
public class MassiliaProduct extends AbstractProduct {
@Override
public String getEan() { return this.ean; }
@Override
public String getLibelle() { return this.libelle; }
@Override
public String getPrice() { return this.price; }
@Override
public String getAccroche() { return this.accroche; }
@Override
public String getOldPrice() { return oldPrice; }
@Override
public String getStock() { return stock; }
@Override
public String getDescriptif() {
return descriptif;
}
@Override
public List<MassiliaCharacteristic> getCharacteristics() {
return (List<MassiliaCharacteristic>)characteristics;
}
@Override
public List<MassiliaProductVisual> getVisuals() {
return (List<MassiliaProductVisual>)visuals;
}
}
他们共享这个模型:
package net.worldline.mst.metro.ds.core.model;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.hateoas.core.Relation;
import java.util.List;
@Relation(value = "product", collectionRelation = "product")
public abstract class AbstractProduct extends ResourceSupport {
protected String ean;
protected String libelle;
protected String accroche;
protected String price;
protected String oldPrice;
protected String stock;
protected String descriptif;
protected List<? extends AbstractProductCharacteristic> characteristics;
protected List<? extends AbstractProductVisual> visuals;
public abstract String getEan();
public abstract String getLibelle();
public abstract String getPrice();
public abstract String getAccroche();
public abstract String getOldPrice();
public abstract String getStock();
public abstract List<? extends AbstractProductCharacteristic> getCharacteristics();
public abstract List<? extends AbstractProductVisual> getVisuals();
public abstract String getDescriptif();
public void setEan(String ean) {
this.ean = ean;
}
public void setLibelle(String libelle) {
this.libelle = libelle;
}
public void setPrice(String price) {
this.price = price;
}
public void setAccroche(String accroche) {
this.accroche = accroche;
}
public void setOldPrice(String oldPrice) {
this.oldPrice = oldPrice;
}
public void setStock(String stock) {
this.stock = stock;
}
public void setCharacteristics(List<? extends AbstractProductCharacteristic> characteristics) {
this.characteristics = characteristics;
}
public void setVisuals(List<? extends AbstractProductVisual> visuals) {
this.visuals = visuals;
}
public void setDescriptif(String descriptif) {
this.descriptif = descriptif;
}
}
在申请中-${profile}.properties
,我精确:
spring.datasource.platform = hsqldb for the jpa instance
.
spring.datasource.platform = none for the instance where i call my webservices.
我的问题很简单:我希望 spring 通过实现存储库让我做我想做的事,但是当我启动我的服务器时,spring 说我的对象不受管理,所以如果我不将 @Entity 添加到我的模型中,它就不会 运行.
那么为什么 Spring 数据看起来像是默认加载 JPA 存储库?
实际上是人为错误。
我在 application.properties
文件中忘记了一个 spring.datasource.platform = hsqldb
。
我没有看它因为我正在使用 spring 配置文件所以我正在查看我的 application-massilia.properties
至极包含 spring.datasource.platform = none
并且现在正在收听因为我已经删除另一个文件中的副本。