Spring crudrepository方法保存 - 无法解析方法

时间:2017-07-10 12:13:34

标签: java spring hibernate web-applications

扩展Spring CrudRepository类时 - 它强制我从界面覆盖save方法。没有收到以下错误我无法调用它: enter image description here

我认为这是一个内置于界面中的默认方法。我能够成功调用repository.findAll()而无需修改任何内容。你能帮忙解释一下吗?

我的回购:

@Repository
@Component
public interface StorageItemRepository extends 
CrudRepository<StorageItemPersistenceModel, Long> {
}

我的模特:

 @Entity
 @Table(name = "StorageItem")
 public class StorageItemPersistenceModel {

/** Reference identifier. */
@Column(name = "ReferenceCode")
private String reference;

/** Date item was stored. */
@Column(name = "DateStored")
private DateTime dateStored;

/** Name of item. */
@Column(name = "Name")
private String name;

/** Item size. */
@Column(name = "Size")
private String size;

/** Image taken of item. */
@Column(name = "Image")
private byte[] image;


/**
 * Gets Image taken of item..
 *
 * @return Value of Image taken of item..
 */
public byte[] getImage() {
    return image;
}

/**
 * Gets Name of item..
 *
 * @return Value of Name of item..
 */
public String getName() {
    return name;
}

/**
 * Gets Date item was stored..
 *
 * @return Value of Date item was stored..
 */
public DateTime getDateStored() {
    return dateStored;
}

/**
 * Sets new Item size..
 *
 * @param size
 *         New value of Item size..
 */
public void setSize(String size) {
    this.size = size;
}

/**
 * Sets new Date item was stored..
 *
 * @param dateStored
 *         New value of Date item was stored..
 */
public void setDateStored(DateTime dateStored) {
    this.dateStored = dateStored;
}

/**
 * Gets Item size..
 *
 * @return Value of Item size..
 */
public String getSize() {
    return size;
}

/**
 * Sets new Image taken of item..
 *
 * @param image
 *         New value of Image taken of item..
 */
public void setImage(byte[] image) {
    this.image = image;
}

/**
 * Sets new Name of item..
 *
 * @param name
 *         New value of Name of item..
 */
public void setName(String name) {
    this.name = name;
}

/**
 * Sets new Reference identifier..
 *
 * @param reference
 *         New value of Reference identifier..
 */
public void setReference(String reference) {
    this.reference = reference;
}

/**
 * Gets Reference identifier..
 *
 * @return Value of Reference identifier..
 */
public String getReference() {
    return reference;
}

/**
 * Equals override.
 *
 * @param obj
 *         obj to compare.
 * @return equals boolean.
 */
@Override
public boolean equals(final Object obj) {
    if (!(obj instanceof StorageItemPersistenceModel))
        return false;
    if (obj == this)
        return true;

    StorageItemPersistenceModel storageItemControllerModel = (StorageItemPersistenceModel) obj;
    return new EqualsBuilder()
            .append(getDateStored(), storageItemControllerModel.getDateStored())
            .append(getReference(), storageItemControllerModel.getReference())
            .append(getName(), storageItemControllerModel.getName())
            .append(getSize(), storageItemControllerModel.getSize())
            .append(getImage(), storageItemControllerModel.getImage())
            .isEquals();
}

@Override
public String toString() {

    final StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.append(reference).append(name).append(size).append(dateStored).append(image);

    return stringBuilder.toString();
}

最后我的gradle依赖项:

dependencies {
testCompile project(':persistence')
compile project(':persistence-model')
compile project(':persistence-mapper')

//test
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'com.github.springtestdbunit', name: 'spring-test-dbunit', version: '1.3.0'
testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
testCompile group: 'org.springframework', name: 'spring-test', version: '4.3.9.RELEASE'

//hibernate
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.10.Final'
compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.10.Final'
compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'


//j2ee
compile('javax.enterprise:cdi-api:2.0')
compile group: 'javax.persistence', name: 'persistence-api', version: '1.0.2'

//spring
compile group: 'org.springframework', name: 'spring-core', version: '4.1.6.RELEASE'
compile group: 'org.springframework', name: 'spring-beans', version: '4.1.6.RELEASE'

//Spring data
compile group: 'org.springframework.data', name: 'spring-data-commons', version: '1.13.4.RELEASE'
compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.11.4.RELEASE'

// sql/db
runtime group: 'mysql', name: 'mysql-connector-java', version: '5.1.21'
}

1 个答案:

答案 0 :(得分:0)

您正在延长CrudRepository<StorageItemPersistenceModel, Long>,但正在尝试保存List<StorageItem>,因此错误未找到save(List<StorageItem>)