I need to write the following with JPA Criteria API:
p.id IS NULL AND nv.organizationalstat IN ('EMPLOYEE', 'FELLOW')`
My code is:
List<String> corePredicateInList = Arrays.asList("EMPLOYEE", "FELLOW");
In<String> corePredicateIn = cb.in(nvRoot.get("organizationalstat"));
corePredicateInList.forEach(p -> corePredicateIn.value(p)); // Setting opts into IN
Predicate p1CorePredicate = cb.and(
cb.isNull(plans.get("id")),
cb.in(nvRoot.get("organizationalstat"), corePredicateIn)
);
The runtime error is
antlr.NoViableAltException: unexpected token: in
...
where ( ( ( ( ( ( generatedAlias0.id is null )
and ( generatedAlias1.organizationalstat in (:param0, :param1) in () ) )
or ( generatedAlias0.id is not null ) )
It looks like there's a double IN somewhere. There are no syntax errors in the code.
I can't do cb.in(List<String>) directly, that's wrong syntax. I have to go through in.value() as indicated in this thread.
Using CriteriaBuilder.In
Predicate p1CorePredicate = cb.and(cb.isNull(plans.get("id")),corePredicateIn);
Or using Expression.In
Predicate p1CorePredicate = cb.and(cb.isNull(plans.get("id")),
nvRoot.get("organizationalstat").in(corePredicateInList));
A solution that worked for me is to do root.in(List<String>) (not cb.in), as follows:
List<String> corePredicateInList = Arrays.asList("EMPLOYEE", "FELLOW");
Predicate p1CorePredicate = cb.and(
cb.isNull(plans.get("id")),
nvRoot.get("organizationalstat").in(corePredicateInList)
);
I am trying to pass selected id to js on click of button.
My code is like this:
array(
'class'=>'CButtonColumn',
'template'=>'{email}',
'buttons'=>array
(
'email' => array
(
'label'=>'Send an e-mail to this user',
'click'=>'function($data->userId){
var y=confirm("Are you sure to send mail to this user?");
if(y==true){
window.location="todoList/sendEmail/$data->userId";
}
}',
),
),
),
Here i am not able to get userid inside js.
What can i do here?
Ok i found out answer to my question myself.My working code is:
array(
'class'=>'CButtonColumn',
'template'=>'{email}',
'buttons'=>array
( 'email' => array
(
'label'=>'Send an e-mail to this user',
'click'=>'function(){
return confirm("Are you sure to send mail to this user?");
}',
'url'=>'Yii::app()->createUrl("todoList/sendEmail", array("id"=>$data->userId))',
),
),
),
I'm working with an inherited a MySQL dB schema that I can't change. I am trying to use DataNucleus (JDO) with it.
I have the following in the schema:
CREATE TABLE `vendors` (
`vendor_id` INT(11) NOT NULL AUTO_INCREMENT,
...
`registration_method` ENUM('Company','Basic Upgrade','Website','Secure Admin','Unknown') NULL DEFAULT NULL,
...
PRIMARY KEY (`vendor_id`),
...
And I have defined a POJO to work with it:
#PersistenceCapable( table="vendors" )
public class VendorRec {
...
public static enum registration_method_enum {
Company( "Company" ), Basic_Upgrade( "Basic Upgrade" ), Website( "Website" ), Secure_Admin( "Secure Admin" ), Unknown( "Unknown" );
private String registration_method;
registration_method_enum( String registration_method ) {
this.registration_method = registration_method;
}
public String getRegistration_method() {
return this.registration_method;
}
};
...
#PrimaryKey
private Integer vendor_id;
...
private registration_method_enum registration_method;
...
I am using DataNucleus's JDO interface to retrieve this class:
Set<Integer> vendorIds = new HashSet();
...
Query q = persistenceManager.newQuery( VendorRec.class );
q.declareParameters( "java.util.Set vendorIds" );
q.setFilter( "vendorIds.contains( vendor_id )" );
q.setOrdering("vendor_fname ascending, vendor_lname ascending");
final Iterator<VendorRec> vendorIt = ( ( List<VendorRec> )q.execute( vendorIds ) ).iterator();
I get the following exception:
java.lang.IllegalArgumentException: No enum constant ca.vendorlink.app.schema.VendorRec.registration_method_enum.Secure Admin
...
Well duh! Of course there isn't an enum constant "registration_method_enum.Secure Admin"! Spaces are not allowed! But that's the dB definition I've inherited... :-(
Suggestions for fixing this problem? Is there a different way I should be defining the enum?
Many thanks in advance.
I have a feeling I'm going about this all wrong. But anyway.
I have an sql database which has essentially a purposefully denormalised table which I've constructed to make this task easier for me, so I can just grab stuff from one table.
What I have is a table of pairs, something like this:
user_lo | user_hi | something_else | other stuff
1000 | 1234 | 1231251654 | 123
1050 | 1100 | 1564654 | 45648
1080 | 1234 | 456444894648 | 1
And so on.
So for my neo4j graph db, I want each user id as a node, the other stuff isn't too important but will be the stuff in the relations basically.
I only want one node for each user, so my feeling is that if I do something like this:
while (rs.next()) {
node_lo = db.createNode();
node_lo.setProperty("user_id", rs.getInt(1));
node_hi = db.createNode();
node_hi.setProperty("user_id", rs.getInt(2));
}
That when we add the node with user_id 1234 for the second time, it will just create a new node, but I what I want is for it to just grab this node instead of creating it so I can add it to the relationship to 1080 in this case.
So what is the way to do this?
Have you looked at CREATE UNIQUE?
If you can't use Cypher, maybe you can use unique nodes?
Use an index to search, and if no result of found, create a new one.
Index<Node> userIndex = graphDatabaseService.index().forNodes('UserNodes');
IndexHits<Node> userNodes = userIndex.get('id', 1234);
if(!userNodes.hasNext()){
//Create new User node
} else {
Node userNode = userNodes.next();
}
Is this the type of operation you are looking for?
You'll probably want to use the UniqueNodeFactory provided by Neo4j.
public Node getOrCreateUserWithUniqueFactory( String username, GraphDatabaseService graphDb )
{
UniqueFactory<Node> factory = new UniqueFactory.UniqueNodeFactory( graphDb, "UserNodes" )
{
#Override
protected void initialize( Node created, Map<String, Object> properties )
{
created.setProperty( "id", properties.get( "id" ) );
}
};
return factory.getOrCreate( "id", id );
}
Normalize your SQL tables to look like nodes and relationships. Then with cypher in your migration you can make the migration rerunnable by something like
start a = node:node_auto_index('id:"<PK_Value>"')
delete a
create a = {id: "<PK_VALUE>", etc}
for nodes and since you should have in your many-to-many middle table:
start LHS = node:node_auto_index('id:"<LHS_PK>"'),
RHS = node:node_auto_index('id:"<RHS_PK>"')
create unique LHS=[:<relType> {<rel props>}]->RHS
now you will end up with no duplicates and can rerun as much as you like.
use this function:
where:
ID is the key which you want to check if already exist
Type: is the type of the node ( the label)
this function will create the node and return it, then you can add more properties.
public static Node getOrCreateUserWithUniqueFactory( long ID, GraphDatabaseService graphDb, String Type )
{
UniqueFactory<Node> factory = new UniqueFactory.UniqueNodeFactory( graphDb, Type )
{
#Override
protected void initialize( Node created, Map<String, Object> properties )
{
created.addLabel( DynamicLabel.label( Type ) );
created.setProperty( "ID", properties.get( "ID" ) );
}
};
return factory.getOrCreate( "ID", ID );
}
using cypher query, you can create a unique node with the following syntax,
CYPHER 2.0 merge (x:node_auto_index{id:1})
when making a REST call, one can make batch insertion like
$lsNodes[] = array(
'method'=> 'POST', 'to'=> '/cypher',
'body' => array(
'query' => 'CYPHER 2.0 merge (x:node_auto_index{id_item:{id}})',
'params' => array('id'=>1)
),
'id'=>0
);
$sData = json_encode($lsNodes);
similarly for creating relationships in a batch request, do the following
$lsNodes[] = array(
'method'=> 'POST', 'to'=> '/cypher',
'body' => array(
'query' => 'start a=node:node_auto_index(id={id1}), b = node:node_auto_index(id={id2}) create unique a-[:have{property:30}}]-b;',
'params' => array(
'id1' => 1, 'id2'=> 2
)
),
'id' => 0
);
$sData = json_encode($lsNodes);
I'm trying to use sun-codemodel to generate source code, checking the API for a long time however no luck.
JBlock body2 = method2.body();
JInvocation arg = body2.invoke( "a" ).arg( xxx ).invoke( "c" ).arg( xxx );
only generate the first part, i.e. a(xxx)
Any ideas?
Problem solved by using
JInvocation invoke = JExpr._this()
.invoke( "fun" )
.arg( "arg1" )
.invoke( "fun2" )
.arg( "arg2" );
method.body().add(invoke);