这是我的模特课:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="[exclusive_data]")
public class OrderControlDataBean {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
Long id;
@Column(name = "Body_Code")
String bodyCode;
@Column(name = "Merchant_Item_No")
String merchantItemNo;
@Column(name = "Merchant_No")
String no;
}
这是我的存储库界面
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.mypackage.mss.oas.filetransfer.app.model.OrderControlDataBean;
@Repository
@Transactional
public interface OrderControlRepository extends
JpaRepository<OrderControlDataBean, Long> {
public void deleteAllInBatch();
List<OrderControlDataBean> save(List<OrderControlDataBean>
orderControlDataBeans);
}
以下是调用此保存方法的服务层:
@Service
@EnableScheduling
public class FTPClientService {
private static final Logger logger =
LoggerFactory.getLogger(FTPClientService.class);
try {
// FTP Client connection code here//
BufferedReader reader = new BufferedReader(new
InputStreamReader(inputStream));
String line = null;
while ((line = reader.readLine()) != null) {
OrderControlDataBean orderControlBean = extractOrderControlBean(line);
if (!(orderControlBean.getBodyCode() == null))
if (!(orderControlBean.getMerchantItemNo() == null))
if (!(orderControlBean.getNo() == null))
inputList.add(orderControlBean);
}
success = ftpClient.completePendingCommand();
repository.deleteAllInBatch();
repository.save(inputList);
} catch (IOException ex) {
logger.error("Error: " + ex.getMessage());
ex.printStackTrace();
} catch (Exception e) {
logger.error("Error: " + e.getMessage());
e.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
logger.error("Error while closing the FTP connection.");
ex.printStackTrace();
}
}
logger.info("Loading OrderControl Data Completed Successfully");
}
我正在从ftp数据填充arraylist读取它保存到数据库失败。 无论我做什么,我都会遇到异常:
org.springframework.beans.NotReadablePropertyException:无效的属性&#39; id&#39; bean类[java.util.ArrayList]:在回退访问期间找不到属性字段!
数据库表的id [id] [int] IDENTITY(1,1)NOT NULL。这是ms sql server。
有人可以帮忙吗?
由于
答案 0 :(得分:0)
我认为您需要将Logger
中的方法调用从repository.save(inputList)
更改为repository.saveAll(inputList)
。
JpaRepository
界面将拥有所有必要的方法,而Spring将在幕后进行繁重的工作。事实上,我相信您不需要在OrderControlRepository
中实现任何方法,甚至不需要使用@JpaRepository
注释。实施JpaRepository
就足够了。
在这里,我们可以看到一个简单的Spring:https://spring.io/guides/gs/accessing-data-jpa/。有关详细信息,请访问:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
让我知道这是否有效!