小编bur*_*ete的帖子

仅在字段不为空时验证

我使用JSR303 Spring Validation并且我有以下内容;

@Digits(fraction = 0, integer = 15)
private String tpMobile;
Run Code Online (Sandbox Code Playgroud)

验证说Must be number between 10-15 digits,但问题是这个字段是可选的。因此,当用户将其留空时,也会显示相同的错误消息。

所以我想做的是仅在该字段不为空时才进行验证。如果该字段为空,则跳过验证该字段

java validation spring jsr bean-validation

6
推荐指数
1
解决办法
8370
查看次数

方法中的Spring Data查询无法识别列

我正在使用Spring Data,我创建了包含在"AbstractEntity"中的实体,所有对象都扩展为获取基本列

AbstractEntity:

@MappedSuperclass
public abstract class AbstractEntity implements Serializable{

    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false)
    private Date CreatedDate;

    @PrePersist
    protected void onCreate() {
        UpdatedDate = CreatedDate = new Date();
    }

...
Run Code Online (Sandbox Code Playgroud)

和我的对象/实体

@Entity
public class Trade extends AbstractEntity {
Run Code Online (Sandbox Code Playgroud)

当我尝试使用我的存储库创建方法时 findByCreatedDateAfter(Date date)

我得到一个例外,无法找到该列......?

public interface TradeRepository extends CrudRepository<Trade, Long>{

    public List<Trade> findByCreatedDateAfter(Date date);
}
Run Code Online (Sandbox Code Playgroud)

它编译(如果我使用某些capitolization)但是试图映射查询,我得到:

Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [createdDate] on this ManagedType [streaming.data.AbstractEntity]
Run Code Online (Sandbox Code Playgroud)

我还想返回sum(amount)此期间的金额列之一.

hibernate spring-data

6
推荐指数
1
解决办法
1万
查看次数

Spring Data JPA通过从父级获取id与父级实体一起插入子级

我想通过调用父对象保存将父实体和子实体一起保存到MySQL数据库中.父实体和子实体之间存在一对一的映射.父ID是自动生成的,我们需要在子项中使用它作为子项的pk.

我使用的是Spring Data JPA 2.0(JPA提供程序是Hibernate)和Spring MVC框架.当试图插入实体时,我收到以下错误.

根本原因

org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.serro.cbmapi.model.Child.parent; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.serro.cbmapi.model.Child.parent org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:207)
Run Code Online (Sandbox Code Playgroud)

这是我的数据库架构:

Parent Table:
   CREATE TABLE `parent` (
   `pid` int(11) NOT NULL AUTO_INCREMENT,
   `parent_name` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
    PRIMARY KEY (`pid`)    ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;

    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Child Table:
    CREATE TABLE `child` (
    `cid` int(11) NOT NULL,
    `child_name` varchar(256) CHARACTER SET utf8 COLLATE …
Run Code Online (Sandbox Code Playgroud)

java hibernate parent-child jpa-2.0 spring-data-jpa

6
推荐指数
1
解决办法
1万
查看次数

使用java查找和替换文本文件中的单词

我试图使用java查找和替换文本文件中的某些单词.我的代码工作到一定程度,但我得到的输出是错误的.我需要使用用户输入替换文本文件中的一行中的多个单词,但是当我运行我的代码时,该行会为我要替换的每个单词复制一次.

例如,如果我想替换以下3个单词:

python ycsb phase db -s -P /home/james/YCSB/workloads/workloada -p 
db.url=db://IP:port -p db.database=name
Run Code Online (Sandbox Code Playgroud)

我最终获得了3行副本,每行都替换了不同的单词.而不是1行替换所有3个所需的单词.代码如下,提前感谢.

public static void main(String[] args) {

    System.out.print("Phase: ");
    Scanner sp = new Scanner(System.in);
    String p = sp.nextLine();

    System.out.print("Database: ");
    Scanner sd = new Scanner(System.in);
    String d = sd.nextLine();

    System.out.print("IP address: ");
    Scanner sip = new Scanner(System.in);
    int ip = sip.nextInt();

    try {
        File file = new File("C://users//James//Desktop//newcommand.txt");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = "", oldtext = "";
        while((line = reader.readLine()) != null) { …
Run Code Online (Sandbox Code Playgroud)

java

6
推荐指数
1
解决办法
3万
查看次数

Java8 LocalDateTime到XMLGregorianCalender删除"+05:30"部分

如下所示,

LocalDateTime currentUTCTime = LocalDateTime.now(ZoneId.of("UTC"));
String reqPattern = currentUTCTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS"));
System.out.println("Required pattern: " + reqPattern);
GregorianCalendar calendar = GregorianCalendar.from(currentUTCTime.atZone(ZoneId.systemDefault()));
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
System.out.println("But Showing As :" + xcal);
Run Code Online (Sandbox Code Playgroud)

我希望输出为2015-06-18 11:59:15:135,但是当我设置xcal为XML标记时XMLGregorianCalender,它显示为2015-06-18T11:59:15.135+05:30.

我该如何删除该+05:30部分?

java-8 java-time

6
推荐指数
1
解决办法
7443
查看次数

Using a map to set parameters for a rest call using RestTemplate

I am currently using a piece of code to set parameters and I do a REST call to a URL using restTemplate, it works fine:

MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("grant_type", grantType);
map.add("client_id", clientId);
map.add("client_secret", clientSecret);
HttpEntity<?> entity = new HttpEntity<Object>(map);
restTemplate.exchange("myurl", HttpMethod.POST, entity, Void.class);
Run Code Online (Sandbox Code Playgroud)

But if I am using a LinkedMultiValueMap it's because I looked on the web ;)

And if I replace it by a HashMap, it works as well, so can anyone tell me …

java spring spring-mvc spring-restcontroller

6
推荐指数
2
解决办法
1万
查看次数

Spring Data Rest:在一对多关系中调用后,外键更新为null

我正在使用spring-data-rest

update并且daily_update是具有一对多关系的2表。使用Spring Boot运行此应用程序。

当我使用发布请求添加数据时,条目被添加到两个表中而没有任何错误,但是在子表(daily_update)列中,“ update_id”(更新表的外键)即将到来null

我正在使用Lombok二传手和吸气剂。

你能帮我吗?

UpdateEntity类:

@Data
@Entity
@Table(name = "update")
public class UpdateEntity {

    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    private String id;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "start_time")
    private Date startTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "end_time")
    private Date endTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "date_created")
    private Date dateCreated;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "date_modified") …
Run Code Online (Sandbox Code Playgroud)

java spring-data spring-data-jpa spring-data-rest

6
推荐指数
2
解决办法
3397
查看次数

无法从配置文件配置附加系统属性

我有Maven故障安全插件的构建配置,其中包括systemPropertyVariables

<build>
  <plugins>
    <plugin>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.19.1</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <systemPropertyVariables>
            <buildDirectory>${project.build.directory}</buildDirectory>
          </systemPropertyVariables>
        </configuration>
      </plugin>
  </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

我也有一个配置文件,可以通过pluginManagement以下方式将少量属性附加到同一插件:

<profile>
  <id>test</id>
  <activation>
    <property>
      <name>test.host</name>
    </property>
  </activation>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-failsafe-plugin</artifactId>
          <configuration>
            <systemPropertyVariables>
              <TEST_HOST>test.host</TEST_HOST>
            </systemPropertyVariables>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</profile>
Run Code Online (Sandbox Code Playgroud)

但是我无法从此配置文件中读取属性。如果我systemPropertyVariables在构建插件配置部分中删除并保留配置文件的pluginManagement配置,则可以正常工作。看来,我需要合并这些属性一起使用时,所以我尝试添加combine.children="append"combine.self="append"configuration插件的,但它并没有帮助。

更新: 我已经找到了导致此问题的原因:我没有提到我的父pom包括这个,而父pom.xml有以下几行:

<build>
  <plugins>
    <plugin>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.19.1</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <systemPropertyVariables>
            <buildDirectory>${project.build.directory}</buildDirectory>
          </systemPropertyVariables> …
Run Code Online (Sandbox Code Playgroud)

pom.xml maven maven-profiles

6
推荐指数
1
解决办法
179
查看次数

禁用 Jupyter Notebook 单元格搜索

我想在 Jupyter 笔记本中禁用单元格搜索:

在此处输入图片说明

这是必然的,cmd + f所以我觉得它非常烦人,因为大多数浏览器都具有出色的内置搜索功能。我查看了“帮助 - > 编辑键盘快捷键”,但找不到它。

macos jupyter-notebook

6
推荐指数
1
解决办法
400
查看次数

使用Spring Boot的Couchbase的通用类型库

我在Spring Boot中创建了一个实用程序,用于以更通用的方式将数据连接和插入/添加到沙发床中。

我有这样的事情:

public interface GenericRepository extends CouchbaseRepository<MyClass, String> {
}
Run Code Online (Sandbox Code Playgroud)

在那里我有MyClass我愿意接受任何形式的文件插入到couchbase。

我尝试了一些类似使用通用类型T的操作,但没有成功,因为出现以下错误:

原因:org.springframework.data.mapping.MappingException:找不到类型类java.lang.Object的PersistentEntity!

我的结构是:服务(接口/ Impl)> DAO(接口/ Impl)>存储库

额外信息:在上述模型中,我传递了通用类型T。我通过带有@Document注释的Pojo调用服务。

目标是消除每种文档类型具有一个存储库类的“依赖性”。

java generics couchbase spring-boot spring-data-couchbase

6
推荐指数
1
解决办法
155
查看次数