Java 仅当url不存在时才插入SQL/Hibernate

Java 仅当url不存在时才插入SQL/Hibernate,java,sql,hibernate,mariadb,Java,Sql,Hibernate,Mariadb,我有一个URL列表和一个包含URL的表。我只想在url不在表中时插入 Data in the Table: |id | url | ... | |---| --- | --- | | 1 | example.com | ... | 我当前的尝试是使用findByURLurl:List方法,并对列表中的每个URL调用此方法。如果返回的列表为空,我会将url插入表中,但不幸的是,我的语句在example.com和example.com之间造成了差异 @Table(name = "u

我有一个URL列表和一个包含URL的表。我只想在url不在表中时插入

Data in the Table: 
|id | url | ... |
|---| --- | --- |
| 1 | example.com | ... | 

我当前的尝试是使用findByURLurl:List方法,并对列表中的每个URL调用此方法。如果返回的列表为空,我会将url插入表中,但不幸的是,我的语句在example.com和example.com之间造成了差异

@Table(name = "url_to_edit")
@NamedQueries({
        @NamedQuery(name= UrlToEdit.FIND_BY_URL, query = "select urlToEdit from UrlToEdit urlToEdit where urlToEdit.url = :url")
})
@NoArgsConstructor
public class UrlToEdit { ... }
对于我当前的解决方案,该表包含以下行:

Data in the Table: 
|id | url | ... |
|---| --- | --- |
| 1 | example.com | ... | 
| 2 | example.com/ | ... | 
| 3 | example.com/# | ... | 
| 4 | www.example.com/ | ... | 
| 5 | https://www.example.com/ | ... | 
| 6 | example.net | ... | 


我怎么能在sql中说它是相同的呢?或者我需要某种预解析器? 有可能进行批量插入吗?我当前的代码一个接一个地插入

编辑:我有来自一个主机的多个URL。我不能追查这些主机名。
e、 g.example.com/test/example.com/test/和example.com/等。

也许您可以使用以下工具查看是否存在:

从UrlToEdit UrlToEdit中选择counturlToEdit,其中UrlToEdit.url类似%:url%


如果计数器为零,您可以插入

我认为您应该在将URL存储到数据库之前对其进行转换;这样,所有数据都将被规范化,而不必手动检查每一行。对表中的url列使用唯一约束也会有所帮助

就转换而言,我认为不能保证以下正则表达式可能有效:

模式URL\u REGEX=Pattern.compile?:https?:\\/\\/?www\\.?[^\/]+.*; 字符串url=http://www.example.com/xxx; Matcher Matcher=URG_REGEX.matcherrl; 如果匹配器匹配{ url=matcher.group2; }

注释:为了适应您的数据,我使用正则表达式,但我不认为ExpLo.com和www. ExqPo.com是相同的URL。< /P> URL1:ExpLo.COM/URL2:Excel。COM/Test/URL2在数据库中。如果我理解正确的查询,那么如果我在第一个url之后询问,结果将是一个。但它应该是零。字段URL是唯一的。我已将我们的想法添加到代码中。我的URL现在总是从www开始,总是没有任何/或。你不必手动检查每一行,你能给我举个例子吗?如果我没有所有的检查要做,我会很感激。我的意思是,如果你的所有url都在数据库中进行了规范化,那么检查给定url是否已经存在的方法非常简单,只需findByUrl。

@Table(name = "url_to_edit")
@NamedQueries({
        @NamedQuery(name= UrlToEdit.FIND_BY_URL, query = "select urlToEdit from UrlToEdit urlToEdit where urlToEdit.url = :url")
})
@NoArgsConstructor
public class UrlToEdit { ... }
Data in the Table: 
|id | url | ... |
|---| --- | --- |
| 1 | example.com | ... | 
| 2 | example.com/ | ... | 
| 3 | example.com/# | ... | 
| 4 | www.example.com/ | ... | 
| 5 | https://www.example.com/ | ... | 
| 6 | example.net | ... |