Select by ResultHandler will put a empty list objcet in localCache - mybatis

MyBatis version
3.5.4
Database vendor and version
MySQL 5.7.14
Test case or example project
public static final String URL = "jdbc:mysql://localhost:3306/myTest?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
public static final String Driver = "com.mysql.cj.jdbc.Driver";
public static final String User_Name = "root";
public static final String Password = "";
public static void main(String[] args) {
DataSource dataSource = new PooledDataSource(Driver,URL, User_Name,Password);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.select("selectCount",resultContext -> {
System.out.println(resultContext.getResultCount());
System.out.println(resultContext.getResultObject());
});
Object c = sqlSession.selectOne("selectCount");
System.out.println(c);
}
public interface BlogMapper {
#Select("select count(*) from datas")
public int selectCount();
}
Steps to reproduce
Expected result
Object c = sqlSession.selectOne("selectCount");
selectOne function should retuen result ,but return null.
Actual result
sqlSession.selectOne("selectCount"); return null

Related

java.sql.SQLSyntaxErrorException in mybatis

The phenomenon and background of the problem encountered
Problems encountered when configuring mybatis and writing tests, an error will be reported as soon as you click to run
problem related code,
#Test
public void findmany() throws IOException
{
InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession =sqlSessionFactory.openSession();
Map<String,Object> params = new HashMap<>();
params.put("name", "sam");
params.put("major", "");
List<Student> student=sqlSession.selectList("com.javaee.pojo.Student.findmany",params);
System.out.println(student);
Map<String,Object> params2 = new HashMap<>();
params2.put("name", "");
params2.put("major", "math");
student=sqlSession.selectList("com.javaee.pojo.Student.findmany",params2);
System.out.println(student);
Map<String,Object> params3 = new HashMap<>();
params3.put("name", "");
params3.put("major", "");
student=sqlSession.selectList("com.javaee.pojo.Student.findmany",params3);
System.out.println(student);
sqlSession.close();
}
mapper
<select id="findmany"
parameterType="map"
resultType="com.javaee.pojo.Student">
select * from students where name like concat('%',#{name},'%') major like concat('%',#{major},'%')
</select>
Student Class
public class Student {
private int id;
private String name;
private String major;
private String sno;
public String toString()
{
return "Student{"+"id="+id+",sno='"+sno+'\''+",name='"+name+'\''+",major='"+major+'\''+'}';
}
Running results and error content
enter image description here
Missing an AND in your select, try this way:
select * from students where name like concat('%',#{name},'%') AND major like concat('%',#{major},'%')

why embedded mongo process not stoping by mongodExecutable.stop()?

Embedded mongo started successful with below code,,but not stoping by mongodExecutable.stop(),this suppose to stop the running mongo. what can be the issue,
The below code
public class Test
{
private static final String CONNECTION_STRING = "mongodb://%s:%d";
private static MongodExecutable mongodExecutable;
ObjectMapper mapper = new ObjectMapper();
private static MongodProcess mongod = null;
#After
public void clean() {
//mongodExecutable.stop();
if(mongod.isProcessRunning()) {
//mongod.stopInternal();
mongodExecutable.stop();
}}
#Before
public void setup() throws Exception {
String ip = "localhost";
int port = 27017;
IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION)
.net(new Net(ip, port, Network.localhostIsIPv6()))
.build();
MongodStarter starter = MongodStarter.getDefaultInstance();
mongodExecutable = starter.prepare(mongodConfig);
mongod=mongodExecutable.start();
}
#Test
public void testfeature() throws Exception {
assertEquals(1, 1);
}
}

Custom DynamoDb TableNameResolver not being called when using CrudRepository

I am testing DynamoDB tables and want to set up different table names for prod and dev environment using the keyword"dev" for development and prod for production.
I have a POJO
#DynamoDBTable(tableName = "abc_xy_dev_MyProjectName_Employee")
public class Employee implements Cloneable {
}
On Prod I want its name to be abc_xy_prod_MyProjectName_Employee.
So, I wrote a TableNameResolver
public static class MyTableNameResolver implements TableNameResolver {
public static final MyTableNameResolver INSTANCE = new MyTableNameResolver();
#Override
public String getTableName(Class<?> clazz, DynamoDBMapperConfig config) {
final TableNameOverride override = config.getTableNameOverride();
String tableNameToReturn = null;
if (override != null) {
final String tableName = override.getTableName();
if (tableName != null) {
System.out.println("MyTableNameResolver ==================================");
return tableName;
}
}
String env = System.getenv("DEPLOYMENT_ENV");
for(Annotation annotation : clazz.getAnnotations()){
if(annotation instanceof DynamoDBTable){
DynamoDBTable myAnnotation = (DynamoDBTable) annotation;
if ("production".equals(env)){
tableNameToReturn = myAnnotation.tableName().replace("_dev_", "_prod_");
}
else {
tableNameToReturn = myAnnotation.tableName();
}
}
}
return tableNameToReturn;
}
}
This works by creating a table with the name abc_xy_prod_MyProjectName_Employee in production.
However, I have a repository with the following code
#EnableScan
public interface EmployeeRepository extends CrudRepository<Employee, String>
{
#Override
<S extends Employee> S save(S employee);
Optional<Employee> findById(String id);
#Override
List<Employee> findAll();
Optional<Employee> findByEmployeeNumber(String EmployeeNumber);
}
Thus when i try to call the method findAll via a endpoint /test case, i get the exception
There was an unexpected error (type=Internal Server Error,
status=500). User:
arn:aws:iam::87668976786:user/svc_nac_ps_MyProjectName_prod is not
authorized to perform: dynamodb:Scan on resource:
:table/abc_xy_dev_MyProjectName_Employee (Service: AmazonDynamoDBv2;
Status Code: 400; Error Code: AccessDeniedException; Request ID:
aksdnhLDFL)
i.e MyTableNameResolver doesn't get called internally when the respository methods are executed. It still points to table name with the name abc_xy_dev_MyProjectName_Employee given in the annotation #DynamoDBTable(tableName = "abc_xy_dev_MyProjectName_Employee")
You have used spring JPA as persistence dynamoDB Integration.
Below configuration can be used to set table name override as part of spring boot configuration.
Sample example is found in https://github.com/ganesara/SpringExamples/tree/master/spring-dynamo
Map Dynamo db repository with user defined mapper config reference
#EnableDynamoDBRepositories(basePackages = "home.poc.spring", dynamoDBMapperConfigRef="dynamoDBMapperConfig")
Mapper Config for table override is as below
#Bean
public DynamoDBMapperConfig dynamoDBMapperConfig() {
DynamoDBMapperConfig mapperConfig = new DynamoDBMapperConfig
.Builder()
.withTableNameOverride(DynamoDBMapperConfig.TableNameOverride.withTableNamePrefix("PROD_"))
.build();
return mapperConfig;
}
Full configuration for reference
#Configuration
#EnableDynamoDBRepositories(basePackages = "home.poc.spring", dynamoDBMapperConfigRef="dynamoDBMapperConfig")
public class DynamoDBConfig {
#Value("${amazon.dynamodb.endpoint}")
private String amazonDynamoDBEndpoint;
#Value("${amazon.aws.accesskey}")
private String amazonAWSAccessKey;
#Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey;
#Bean
public AmazonDynamoDB amazonDynamoDB() {
AmazonDynamoDB amazonDynamoDB
= new AmazonDynamoDBClient(amazonAWSCredentials());
if (!StringUtils.isEmpty(amazonDynamoDBEndpoint)) {
amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint);
}
return amazonDynamoDB;
}
#Bean
public AWSCredentials amazonAWSCredentials() {
return new BasicAWSCredentials(
amazonAWSAccessKey, amazonAWSSecretKey);
}
#Bean
public DynamoDBMapperConfig dynamoDBMapperConfig() {
DynamoDBMapperConfig mapperConfig = new DynamoDBMapperConfig
.Builder()
.withTableNameOverride(DynamoDBMapperConfig.TableNameOverride.withTableNamePrefix("PROD_"))
.build();
return mapperConfig;
}
#Bean
public DynamoDBMapper dynamoDBMapper() {
return new DynamoDBMapper(amazonDynamoDB(), dynamoDBMapperConfig());
}
}
You are using DynamoDBMapper (the Java SDK). Here is how I use it. Lets say I have a table called Users, with an associated User POJO. In DynamoDB I have DEV_Users and LIVE_Users.
I have an environment variable 'ApplicationEnvironmentName' which is either DEV or LIVE.
I create a custom DynamoDBMapper like this:
public class ApplicationDynamoMapper {
private static Map<String, DynamoDBMapper> mappers = new HashMap<>();
private static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withRegion(System.getProperty("DynamoDbRegion")).build();
protected ApplicationDynamoMapper() {
// Exists only to defeat instantiation.
}
public static DynamoDBMapper getInstance(final String tableName) {
final ApplicationLogContext LOG = new ApplicationLogContext();
DynamoDBMapper mapper = mappers.get(tableName);
if (mapper == null) {
final String tableNameOverride = System.getProperty("ApplicationEnvironmentName") + "_" + tableName;
LOG.debug("Creating DynamoDBMapper with overridden tablename {}.", tableNameOverride);
final DynamoDBMapperConfig mapperConfig = new DynamoDBMapperConfig.Builder().withTableNameOverride(TableNameOverride.withTableNameReplacement(tableNameOverride)).build();
mapper = new DynamoDBMapper(client, mapperConfig);
mappers.put(tableName, mapper);
}
return mapper;
}
}
My Users POJO looks like this:
#DynamoDBTable(tableName = "Users")
public class User {
...
}
When I want to use the database I create an application mapper like this:
DynamoDBMapper userMapper = ApplicationDynamoMapper.getInstance(User.DB_TABLE_NAME);
If I wanted to a load a User, I would do it like this:
User user = userMapper.load(User.class, userId);
Hope that helps.

Using Green DAO with content provider get error

I use GreenDao to generate ContentProvider and when I trying to use it went wrong.it tell me "DaoSession must be set during content provider is active".I dont know where to set the DaoSession.
ContentProvider class as follows
public class ContactContentProvider extends ContentProvider {
public static final String AUTHORITY = "com.junsucc.www.provider";
public static final String BASE_PATH = "contact";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
+ "/" + BASE_PATH;
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
+ "/" + BASE_PATH;
private static final String TABLENAME = ContactDao.TABLENAME;
private static final String PK = ContactDao.Properties.Id
.columnName;
private static final int CONTACT_DIR = 0;
private static final int CONTACT_ID = 1;
private static final UriMatcher sURIMatcher;
static {
sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sURIMatcher.addURI(AUTHORITY, BASE_PATH, CONTACT_DIR);
sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", CONTACT_ID);
}
public DaoSession daoSession=BaseApplication.getDaoSession();
#Override
public boolean onCreate() {
// if(daoSession == null) {
// throw new IllegalStateException("DaoSession must be set before content provider is created");
// }
DaoLog.d("Content Provider started: " + CONTENT_URI);
return true;
}
protected SQLiteDatabase getDatabase() {
if (daoSession == null) {
throw new IllegalStateException("DaoSession must be set during content provider is active");
}
return daoSession.getDatabase();
}
......
the error as follow
java.lang.IllegalStateException: DaoSession must be set during content provider is active
at com.junsucc.www.ContactContentProvider.getDatabase(ContactContentProvider.java:71)
at com.junsucc.www.ContactContentProvider.insert(ContactContentProvider.java:83)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:220)
at android.content.ContentResolver.insert(ContentResolver.java:1190)
at com.junsucc.junsucc.MD5UtilsTest.testProvider(MD5UtilsTest.java:58)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
but I had setted th DaoSession inside my Application
public class BaseApplication extends Application {
private static Context mContext;
private static DaoMaster mDaoMaster;
private static DaoSession mDaoSession;
public static DaoMaster getDaoMaster() {
return mDaoMaster;
}
public static Context getContext() {
return mContext;
}
#Override
public void onCreate() {
mContext = getApplicationContext();
DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(mContext, Constants.DB_NAME, null);
mDaoMaster = new DaoMaster(helper.getWritableDatabase());
mDaoSession = mDaoMaster.newSession();
super.onCreate();
}
}
Follow the advice of the framework
/**
* This must be set from outside, it's recommended to do this inside your Application object.
* Subject to change (static isn't nice).
*/
public static DaoSession daoSession;
In your applicaction code
#Override
public void onCreate() {
super.onCreate();
DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(this, Constants.DB_NAME, null);
mDaoMaster = new DaoMaster(helper.getWritableDatabase());
mDaoSession = mDaoMaster.newSession();
/***********************************************/
ContactContentProvider.daoSession = mDaoSession;
/***********************************************/
}
Because ContentProvider is created ahead of Application.
So daoSession will be null when ContentProvider created.

Null pointer exception when creating custom Dao with ormlite-android

I'm trying to extend the base ORMLite DAO class so I can add some custom methods. I've tried following the answer here, but I'm getting a null error and not sure how to cast the dao object correctly (Ormlite - Constructor call failing when BaseDaoImpl is extended) Currently, I have the following table:
#DatabaseTable(tableName="beers", daoClass=BeerDao.class)
public class Beer {
public static final String BEER_NAME = "name";
#DatabaseField(generatedId = true)
private UUID id = UUID.randomUUID();
#DatabaseField()
private String name;
#DatabaseField()
private String breweryName;
public Beer() {}
... getters/setters
}
The BeerDao class:
public class BeerDao<Beer,UUID> extends BaseDaoImpl<Beer,UUID> {
public BeerDao(ConnectionSource connectionSource, Class<Beer> dataClass) throws SQLException {
super(connectionSource, dataClass);
}
}
DatabaseHelper code:
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final String TAG = "Database";
private static final String DATABASE_NAME = "brewgenius.db";
private static final int DATABASE_VERSION = 7;
private BeerDao<Beer, UUID> beerDao = null;
private Dao<Checkin, UUID> checkinDao = null;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
}
/* ... onCreate and onUpgrade code ..*/
/**
* Get Beer Model DAO
*
* #return Beer DAO
*/
public BeerDao<Beer, UUID> getBeerDao() {
if (beerDao == null) {
try {
beerDao = getDao(Beer.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
return beerDao;
}
}
In my activity, when I try to get the Dao, I get a null pointer exception.
BeerDao<Beer,UUID> dao = getHelper().getBeerDao();
FYI, Dao is cast to:
BeerDao<Beer,UUID>
UPDATE
It looks like my DatabaseConfigUtil isn't reading the DaoClass attribute.
public class DatabaseConfigUtil extends OrmLiteConfigUtil {
public static void main(String[] args) throws SQLException, IOException {
writeConfigFile("ormlite_config.txt");
}
}
Removing ormlite_config.txt from DatabaseHelper's constuctor caused on-the-fly reading which correctly reads the DaoClass attribute. Any idea why the writer isn't writing this?
Here's ormlite_config.txt
# --table-start--
dataClass=com.brewgenius.model.Beer
tableName=beers
# --table-fields-start--
# --field-start--
fieldName=id
columnName=_id
generatedId=true
# --field-end--
# --field-start--
fieldName=name
# --field-end--
# --field-start--
fieldName=breweryName
# --field-end--
# --table-fields-end--
# --table-end--
#################################