# (rdf4j-stardog) StardogRepositoryConnection::hasStatement always returns true when passing BNodes

**URL:** <https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-hasstatement-always-returns-true-when-passing-bnodes/1296>\
**Category:** Support\
**Created:** [September 17, 2018, 7:41am UTC](https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-hasstatement-always-returns-true-when-passing-bnodes/1296 "2018-09-17T07:41:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MiguelAraCo](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/miguelaraco/32/306_2.png) [@MiguelAraCo](https://community.stardog.com/u/MiguelAraCo)\
**Post date:** [September 17, 2018, 7:41am UTC](https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-hasstatement-always-returns-true-when-passing-bnodes/1296/1 "2018-09-17T07:41:56Z")

</div>

The following code will always return `true` regardless of the existence of statements with that subject:

```auto
BNode subject = SimpleValueFactory.getInstance().createBNode( "some-id" );
connection.hasStatement( subject, null, null, false );

```

In the past this code used to work since the implementation of `hasStatement` was the one that `AbstractRepositoryConnection` has, but recently this was changed to use a SPARQL query instead. Since SPARQL queries can't reference BNodes directly, the subject gets replaced by a SPARQL variable and returns true always.

## Workaround

To make it work, we changed the implementation of the method to:

```auto
public boolean hasStatement( Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts ) throws RepositoryException {
	StringBuilder triplePattern = new StringBuilder( "{ " );
	StringBuilder auxiliaryPatterns = new StringBuilder();

	// Subject
	if ( subj == null ) {
		triplePattern.append( " ?s" );
	} else if ( subj instanceof BNode ) {
		triplePattern.append( " ?s" );
		auxiliaryPatterns.append( " FILTER( STR( ?s ) = \"" ).append( ( (BNode) subj ).getID() ).append( "\" ) ." );
	} else {
		triplePattern.append( " " ).append( SesameQueryUtils.getSPARQLQueryString( APIMapping.toSesameValue( subj ) ) );
	}

	// Predicate
	if ( pred == null ) {
		triplePattern.append( " ?p" );
	} else {
		triplePattern.append( " " ).append( SesameQueryUtils.getSPARQLQueryString( APIMapping.toSesameValue( pred ) ) );
	}

	// Object
	if ( obj == null ) {
		triplePattern.append( " ?o" );
	} else if ( obj instanceof BNode ) {
		triplePattern.append( " ?o" );
		auxiliaryPatterns.append( " FILTER( STR( ?o ) = \"" ).append( ( (BNode) obj ).getID() ).append( "\" ) ." );
	} else {
		triplePattern.append( " " ).append( SesameQueryUtils.getSPARQLQueryString( APIMapping.toSesameValue( obj ) ) );
	}

	triplePattern.append( " ." )
	             .append( auxiliaryPatterns )
	             .append( " }" );

	if ( contexts.length > 0 ) {
		for ( Resource context : contexts ) {
			String query = String.format( "ASK { GRAPH <%s> %s }", context.stringValue(), triplePattern );
			if ( this.mConnection.ask( query ).reasoning( includeInferred ).execute() ) return true;
		}

		return false;
	} else {
		String query = String.format( "ASK %s", triplePattern );
		return this.mConnection.ask( query ).reasoning( includeInferred ).execute();
	}
}

```

## Environment

- Stardog: 5.3.4
- rdf4j-stardog: 5.3.4

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [October 2, 2018, 4:33pm UTC](https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-hasstatement-always-returns-true-when-passing-bnodes/1296/3 "2018-10-02T16:33:52Z")

</div>

Hi Miguel,

Thank you very much for the detailed bug reports both here and in your [other topic](https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-getstatements-wont-return-the-correct-statements-when-passing-bnodes/1297).

We have tracked down the underlying cause for these issues and the fix for them will be included in the 5.3.5 release.

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [October 2, 2018, 4:34pm UTC](https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-hasstatement-always-returns-true-when-passing-bnodes/1296/4 "2018-10-02T16:34:35Z")

</div>



---

<div class="post-metadata">

**Author:** ![mhernandeza](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@mhernandeza](https://community.stardog.com/u/mhernandeza)\
**Post date:** [September 11, 2019, 10:40pm UTC](https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-hasstatement-always-returns-true-when-passing-bnodes/1296/5 "2019-09-11T22:40:58Z")

</div>

Hi Stephen,

I was just reviewing this solution and was wondering if you would recommend the filtering process first stated `FILTER ( STR( ?s ) = "b0" )` or if an alternative like the following would be a more appropriate solution: `FILTER( ?s = bnode( "b0" ) )`.

Also, I am using this code along with Stardog's 7.0.1 release and I was wondering if there was a particular reason as to why this issue wasn't resolved.

This is also related to the [other topic](https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-getstatements-wont-return-the-correct-statements-when-passing-bnodes/1297) you mentioned in your comment.

Thanks for your continuous support!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [April 1, 2022, 1:33am UTC](https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-hasstatement-always-returns-true-when-passing-bnodes/1296/6 "2022-04-01T01:33:55Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [April 7, 2022, 8:28pm UTC](https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-hasstatement-always-returns-true-when-passing-bnodes/1296/7 "2022-04-07T20:28:43Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [April 14, 2022, 10:56pm UTC](https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-hasstatement-always-returns-true-when-passing-bnodes/1296/8 "2022-04-14T22:56:55Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [April 23, 2022, 10:51am UTC](https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-hasstatement-always-returns-true-when-passing-bnodes/1296/9 "2022-04-23T10:51:41Z")

</div>



---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [May 2, 2022, 6:47am UTC](https://community.stardog.com/t/rdf4j-stardog-stardogrepositoryconnection-hasstatement-always-returns-true-when-passing-bnodes/1296/10 "2022-05-02T06:47:16Z")

</div>


