I want to make a simple query with a getValueByLabel Method:
Here is my code:
public Config getValueByLabel(String label) throws EntityPersistException{
try {
Query query = em.createQuery("select id from config where config_label=:label",Long.class);
query.setParameter("label", label);
List<Long> config = query.getResultList();
return em.getReference(Config.class, config.get(0));
}
...
when I want to start the method I get:
org.hibernate.hql.internal.ast.QuerySyntaxException: config is not mapped [select id from config where config_label=:label]
Any ideas how to fix that?
UPDATE
I am using:
hibernate 4.0.1.Final
and a postgresql db 1.16.1
syntax of hql is case sensitive. please see if table/entity and column/instance variable names used in query are same as that of object.
Related
I'm new to using EF to handle data in SQL. In a MVC Core project we're testing EF (Microsoft.EntityFrameworkCore, version 2.2.3) to handle data.
When trying to update data and update failed for some reason (missing fields etc) it seemed like EF actually deleted the record from the database (MSSQL 2014) instead of throwing an update error...
Is it possible?
Code for updating:
public void Update(Contact contact)
{
_dbContext.Update(contact);
_dbContext.SaveChanges();
}
When trying to update data and update failed for some reason (missing fields etc) it seemed like EF actually deleted the record from the database (MSSQL 2014) instead of throwing an update error...
Is it possible?
It should not.
test it, try to debug here
_dbContext.Update(contact);
_dbContext.SaveChanges();
var updated = _dbContext.Contacts.FirstOrDefault(x => x.Id == contact.Id); //debug here
check if it has a value, if still none, these are the scenarios i can think of that may have caused your problem
investigate the missing field specially if it is not nullable.
is the _dbContext used here is the same connection string used with everything?
is the [Key] attribute listed on your Contact entity?
public class Contact
{
[Key]
public int Id
}
overridden the SaveChanges function?
is what you are passing Contact contains a Key and it is not 0?
is a delete function called after Update?
try using SQL Profiler to look at the Linq to SQL if it really generated an update query and if it is really pointing at the right [Key]
but if it is still not working properly, you could do
public void Update(Contact contact)
{
var selectedContactToBeUpdated = _dbContext.Contacts.FirstOrDefault(x => x.Id == contact.Id);
if (selectedContactToBeUpdated != null)
{
selectedContactToBeUpdated.PropertyToBeUpdated1 = newValue;
selectedContactToBeUpdated.PropertyToBeUpdated2 = newValue2;
//additional Properties
_dbContext.SaveChanges();
}
}
in the scenario above, it will only generate an Update statement with fields you have changed.
I'm using DBUnit to insert data (dumped from a Postgres DB) into SQL Server, but want to do the insert into schema "rules", not the default "dbo" schema:
Class.forName(net.sourceforge.jtds.jdbc.Driver.class.getName());
Connection sqlsCon = DriverManager.getConnection("jdbc:jtds:sqlserver://5.5.5.5:7000;databaseName=THE_DB", "THE_USER", "THE_PW");
IDatabaseConnection sqlsDbCon = new DatabaseConnection(sqlsCon);
DatabaseOperation.CLEAN_INSERT.execute(sqlsDbCon, partialDataSet);
Thank you!
If you use spring boot 2 then you can provide a custom configuration in your config:
#TestConfiguration
public class MyDbUnitConfiguration {
#Bean
public DatabaseDataSourceConnectionFactoryBean dbUnitDatabaseConnection(DataSource dataSource) {
DatabaseConfigBean databaseConfig = new DatabaseConfigBean();
databaseConfig.setQualifiedTableNames(Boolean.TRUE);
DatabaseDataSourceConnectionFactoryBean databaseDataSourceConnectionFactory =
new DatabaseDataSourceConnectionFactoryBean();
databaseDataSourceConnectionFactory.setDatabaseConfig(databaseConfig);
databaseDataSourceConnectionFactory.setDataSource(dataSource);
return databaseDataSourceConnectionFactory;
}
}
Include this configuration and update your datasets.xml to fully qualified names.
There are a few ways to support that, see the documentation here:
http://dbunit.sourceforge.net/faq.html#AmbiguousTableNameException
For example you could enable the qualified table names property and use the fully qualified table names like SCHEMA.TABLE. Enabling that involves the following code:
conn=getConnection();
conn.getConfig().setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
I currently have a query that returns all the documents in a collection using the findAll() method of MongoTemplate. I want to sort these results, but do not see any way to do so. I see that I can use find() with a Query argument and call .with(Sort sort), but in this scenario, how do I set the Query to return all documents? I would be okay with using either approach.
Query query = new Query();
query.with(new Sort(Sort.Direction.DESC, "_id"));
List<MyClass> myClassList= mongoTemplate.find(query, MyClass.class);
An empty Query will behave as findAll(). Like you can write in the mongo shell: db.myCollection.find({}) you can write an emypty Query in the java mongdb driver.
An working sample code would be:
public static void main(String[] args) throws UnknownHostException
{
ServerAddress address = new ServerAddress("localhost", 27017);
MongoClient client = new MongoClient(address);
SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(client, "mydatabase");
MongoTemplate mongoTemplate = new MongoTemplate(simpleMongoDbFactory);
Query query = new Query().with(new Sort("_id", "-1"));
List<MyClass> allObjects = mongoTemplate.find(query, MyClass.class);
System.out.println(allObjects);
}
The syntax is now:
Query query = new Query();
query.with(Sort.by(Sort.Direction.DESC, "_id"));
List<MyClass> myClassList= mongoTemplate.find(query, MyClass.class);
I want to add new fields to a domain class so that it updates the generated table's attributes set. I have a domain class like this,,
Class Book {
String name
static constraints = {
name nullable:false
}
String toString() {
return name
}
}
The GORM generated includes name along with id and version. Now I want to add ISBN to Book domain class as following
Class Book {
String name
String ISBN
static constraints = {
name nullable:false
ISBN nullable:true
}
String toString() {
return name
}
}
I don't want to use create-drop in DataSource.groovy because it will delete all my previous data. My DataSource.groovy looks like this,,
dataSource {
dbCreate = "update"
url = "jdbc:jtds:sqlserver://localhost:1433/databaseName"
username = "username"
password = "password"
}
I want GORM to add ISBN field to book table. But its not happening. Where am I wrong ?
I am using sql server 2008 and grails version 2.1.1
dataSource {
dbCreate = "update"
}
should do the trick. It updates you db without dropping the tables
you will need to crate this field in sql server by hand it use to be a easy task in sqlserver because all its tools, and you should take a look to grails database migration plugin documentation. It should be already installed in your grails version
I am using VS2012, .NET 4.5 and SolrNet. I am struggling with solrnet mappings. I've succesfully started Apache Solr with jetty on http://localhost:8983/solr. My class which I want to add to solr is:
public class Register
{
[SolrUniqueKey("id")]
public string Id { get; set; }
[SolrField("body")]
public string Body { get; set; }
}
I succesfully connect to solr, but I can't put my document into it:
Startup.Init<Register>(solrAddress);
Solr = ServiceLocator.Current.GetInstance<ISolrOperations<Register>>();
var reg = new Register
{
Id = "SP2514N",
Body = #"Dosel je prosel"
};
Solr.Add(reg);
Solr.Commit();
Here I receive error, that 'body' is unknown field. I've also used MappingManager, like this:
var mgr = new MappingManager();
var property = typeof(Register).GetProperty("Id");
mgr.Add(property, "id");
mgr.SetUniqueKey(property);
mgr.Add(typeof(Register).GetProperty("Body"), "body");
But, again, my field was not mapped. What am I doing wrong? Isn't the mapping to solr supposed to be done through code? Do I need a special xml file?
You need to confirm that you have a body field defined in your schema. If you are just using the default schema that comes with Solr, it does not include a body field. You can copy an existing similar entry in the schema.xml file, like description to get you going.
For more reference on configuring the Solr schema please refer to the following:
Solr Reference Guide - Overview of Documents, Fields and Schema Design
schema.xml - SolrTutorial.com