我在Spring Boot上創建了一個項目。春季開機不讓我創建一個沒有數據庫的倉庫
我有兩個提供者擴展了相同的抽象提供者,並且我在啓動時加載了我通過Spring Profile感興趣的提供者。
其中一個提供程序基於JPA,另一個提供了他的接口實現,我打電話給webservices。
這是我不希望使用數據庫提供商至極的接口:
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);
}
不使用數據庫的提供者有一個實現由於工作原因,我不能告訴你實現,但它呼籲內部網絡服務
就像我的提供者,我有兩個模型,擴展相同的抽象類。
一個用@ Entity,@ Id和co註解,我不想在另一個類上添加這個註釋,因爲對於我來說,我已經精簡了我不想要任何數據庫在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數據看起來像默認加載JPA存儲庫?
如果您「忘記精確」,您可以使用您的文章的編輯功能。如果你這樣做,你可以將文件內容標記爲代碼,這可以提高可讀性。顯示你的課程也可能有幫助。我確信你希望,不要跳躍;-) – Marged
是啊...如果我可以,但我需要10個添加圖像的聲望,我已經準備好截圖爲空:/ –
將它們作爲文本和格式添加爲代碼。 .. – Marged