I have an issue using jOOQ's MockDataProvider to mock a SQL JOIN in a unit test.
The JOIN implemented with jOOQ selects a single column containing UUIDs. Thus the result type is Result<Record1<UUID>>. In the associated unit test I would like to mock this result using the MockDataProvider, but I can't find a proper way to initialize the Result, as I can't find a way to create a table like object I could pass to the newResult method. I know that there are some table methods in DSL, but the signatures just seem to be wrong for my use case.
Also as the unit test is not runnable at this point, I'm not sure that the way I create the UUID field is correct.
This is my code:
private class MyProvider implements MockDataProvider {
#Override
public MockResult[] execute(final MockExecuteContext ctx) throws SQLException {
final MockResult[] mockResults = new MockResult[1];
final DSLContext db = DSL.using(SQLDialect.POSTGRES);
final Result<Record1<UUID>> result = db.newResult(<some table object here>);
final Field<UUID> uuidField = DSL.fieldByName(UUID.class, "uuid");
final Record1<UUID> record = db.newRecord(uuidField);
record.setValue(uuidField, ID);
result.add(record);
mockResults[0] = new MockResult(1, result);
return mockResults;
}
}
I suspect that this is essentially the same question as the one on the jOOQ User Group here:
https://groups.google.com/forum/#!topic/jooq-user/h4pfIHjmBpo
In summary, there is a method that is going to be added to jOOQ 3.4 (with issue #3139), to help you create such Result objects for arbitrary Record types. In the mean time (before jOOQ 3.4 is released), you will have to resort to creating a new org.jooq.impl.ResultImpl via reflection.
Related
I have a Couchbase-Document "Group" with a list of group-members-names. I want to query for all groups of one person. I managed to do it with N1QL ARRAY_CONTAINS - see in code example - but i hoped that i could generate the query from the method name as it is usual in Spring Data.
Any help is appreciated :)
I tried
public List<MyGroup> findAllByMembers(String member); and public List<MyGroup> findByMembers(String member); but they just return an empty list - i guess they try to match the whole "members" value and don't recognize it as a list -, no errors.
Code
My Document with a List field
#Data
#Document
public class MyGroup {
private String name;
private List<String> members = new ArrayList<>();
}
My Repository
#RepositoryDefinition(domainClass = MyGroup.class, idClass = String.class)
public interface MyGroupRepository extends CouchbaseRepository<MyGroup, String> {
//#Query("#{#n1ql.selectEntity} WHERE ARRAY_CONTAINS(members,$1) AND #{#n1ql.filter}")
public List<MyGroup> findAllByMembers(String member);
}
Expected
Given a "group1" with "member1" in members.
repository.findAllByMembers("member1"); should return ["group1"].
Couchbase is limited by the Spring Data specification. Unfortunately, we can't simply add new behaviors to it (if you switch to a relational database, it has to work with no breaking points). So, whenever you need to query something that has a N1QL specific function/keyword, you have to write a query via #Query
I have two comboboxes connected to a two DatabaseTables in ORMLite. For exammple first combobox shows brands (from brands_table) and second shows models (from models_table). Models_table has also foreignField with brandId. When I'm saving a new record, I must choose brand first then model.
For initializaton and set data in both of comboboxes I use queryForAll method from DAO.
My question is which method I could use for filtering data in second combobox when I already choose something from first. I was thinking about queryForEq and queryForMatching.
For now normal initialize is:
public void initializeModelsList() throws ApplicationException {
CarModelDao carModelDao = new CarModelDao(DBManager.getConnectionSource());
List<CarModel> listOfModels = carModelDao.queryForAll(CarModel.class);
modelsList.clear();
// loop
listOfModels.forEach((c) -> {
// create new object ModelsFX model
ModelsFX modelsFX = ConverterModels.convertToModelFX(c);
this.modelsList.add(modelsFX);
And what I'm thinking about is:
public void initializeFilteredModelsList() throws ApplicationException {
CarBrandDao carBrandDao=new CarBrandDao(DBManager.getConnectionSource());
CarBrand tempCarBrand = carBrandDao.findById(CarBrand.class, this.getModelsFxObjectProperty().getBrandsFXObjectProperty().getId());
//it returns int with ID
CarModelDao carModelDao = new CarModelDao(DBManager.getConnectionSource());
List<CarModel> listOfFilteredModels = carModelDao.//searching for same ID in foreignField(brand_ID) in models_table
listOfFilteredModels.forEach((c) -> {
// create new object ModelsFX model
ModelsFX modelsFX = ConverterModels.convertToModelFX(c);
this.modelsList.add(modelsFX);
});
My question is which method I could use for filtering data in second combobox when I already choose something from first.
If I understand, this is textbook example of foreign objects that is covered very well by the documentation for ORMLite. In the examples from the docs they have Account and Order which in your example would be CarBrand and CarModel.
To quote from the docs:
You can query for foreign fields in a couple of different ways. The following examples show code which queries for all orders that match a certain account field. Since the id field is the name field, you can query by the name field of the account:
// query for all orders that match a certain account
List<Order> results = orderDao.queryBuilder().
where().eq("account_id", account.getName()).query();
Or you can just let ORMLite extract the id field from the account itself. This will perform an equivalent query to the above:
// ORMLite will extract and use the id field internally
List<Order> results = orderDao.queryBuilder().where().eq("account_id", account).query();
Lets assume I have a database table like:
Table Building
ID int (primary key)
title varchar
And to fetch this, I have a Java Class like:
class Building{
private Integer ID;
private String title;
...
}
(Add some JPA annotations in your imagination if that helps you).
But now, depending on the actual Building, I want to execute some logic.
My first thought was to create a big switch/case like:
switch(building.getId()){
case 1:
BuildingA buildingLogic = new BuildingA(building);
break;
case 2:
BuildingB buildingLogic = new BuildingB(building);
break;
...
}
buildingLogic.doBuildingSpecificStuff();
But that would end in an endless switch/case, which would not be very clear to "read".
My next thougt (already covered by the answer of Mark Elliot) was to write the actual Class name into the database as an additional field (BuildingClass) and with some (to me currently unknown) java magic, create the object from the building.getBuildingclass() String, but I already assume that would have some sort of disadvantages...
So I thought maybe you people could give me some more ideas or comments on my thoughts.
Supposing you did have the class name, you could use reflection to instantiate a new instance, though there are some security and performance risks for going down this path.
String className = "com.foo.bar.Baz";
// gets the actual class (might throw an Exception if the class doesn't exist)
Class<?> clazz = Class.forName(className);
// probably want to have some common interface these conform to, so
// let's assume you have one, you can test this is the right kind of class
CommonBuildingType buildingLogic = null;
if (CommonBuildingType.class.isAssignableFrom(clazz)) {
// get the constructor to invoke (might throw if no match)
Constructor constructor = clazz.getDeclaredConstructor(Building.class);
// safe to cast, we checked above
buildingLogic = (CommonBuildingType) constructor.newInstance(building);
} else {
// throw an exception?
}
You should use hibernate to achieve it more sophistically
I'm trying to create a generic DAO in order to avoid having more or less the same code in many separate DAOs.
My problem is that in the following lines of code:
private BaseDAOImpl<Artist> baseDAOArtist = new BaseDAOImpl<>(Artist.class);
private BaseDAOImpl<ArtistRelation> baseDAOArtistRelation = new BaseDAOImpl<>(ArtistRelation.class);
The first one seems to be skipped.
An excerpt of the BaseDAOImpl:
public class BaseDAOImpl<T> implements BaseDAO<T> {
private Class<T> entity;
private DAOFactory daoFactory = Config.getInstance().getDAOFactory();
private static String SQL_FIND_BY_ID;
public BaseDAOImpl(Class entity) {
this.entity = entity;
SQL_FIND_BY_ID = "SELECT * FROM VIEW_" + entity.getSimpleName() + " WHERE id = ?";
}
}
Is it not possible to instantiate multiple objects this way?
Yes. It's not clear what you mean by "The first one seems to be skipped." but it could be that your using a static value for "SQL_FIND_BY_ID"? As at the moment:
private BaseDAOImpl<Artist> baseDAOArtist = new BaseDAOImpl<>(Artist.class);
Creates two instance variables and sets the value of SQL_FIND_BY_ID then:
private BaseDAOImpl<ArtistRelation> baseDAOArtistRelation = new BaseDAOImpl<>(ArtistRelation.class);
Creates two new instance variables and will change the value "SQL_FIND_BY_ID" for both instances.
Without a more detailed description of the error I am more or less guessing now, but judging from variable names and the code snippet I would suspect the static field SQL_FIND_BY_ID to be the cause.
When you instantiate the two DAOs, the second execution of the constructor BaseDAOImpl will overwrite the value of the static field. If the DAO relies on the SQL query stored there, it will always query for the entity of the last instantiated DAO.
Static fields and methods are shared among all instances of a class even if they differ on their generic parameters. In contrast to e.g. C++'s templates, there are no separate classes generated for each generic parameter.
To achieve the desired behavior of separate queries for each entity you may change the static field to a non-static member.
Please be patient with the newbie question as I'm trying to learn MyBatis and java at the same time. I have an application in which I need to use threadsafe variables. Based on some research and my ideas of how the application will be used, I've settled on a CopyOnWriteArrayList over a Vector.
When I call a selectList from the mybatis sql session, is there any way to tell it to create an CopyOnWriteArrayList as its return rather than an ArrayList? Admittedly, my code to configure this is two lines instead of one, but something in me says that there's got to be a better way and/or I'm not the first one to have encountered this.
List<Team> teams = session.selectList("getTeamsByGameID", gameID);
List<Team> arrayListReturn = new CopyOnWriteArrayList<Team>(teams);
return arrayListReturn;
Thanks in advance,
I know of two ways to handle this.
Option 1: Use a Mapper class and specify the type of List to return there.
Define a Mapper interface:
public interface TeamMapper {
CopyOnWriteArrayList<Team> getTeamsByGameID();
}
Your mapper xml file stays the same. The code to do the query changes to:
TeamMapper m = session.getMapper(TeamMapper.class);
List<Team> lt = m.getTeamsByGameID();
System.out.println(lt.getClass());
//=> prints out "class java.util.concurrent.CopyOnWriteArrayList"
Option 2: Create a ResultHandler and pass that into the session.select() method.
Here you use the ResultHandler interface. That interface requires you to override one method, handleResult, which is given each result that comes back from the database as the query is in progress.
In your case, your ResultHandler would look something like this:
public class TeamResultHandler implements ResultHandler {
private List<Team> teams = new CopyOnWriteArrayList<Team>();
#Override
public void handleResult(ResultContext rc) {
countries.add((Team) rc.getResultObject());
}
// provide a getter so you can retrieve it when finished
public List<Team> getTeamList() {
return teams;
}
}
Instead of using selectList as you do above, you would now use the session.select(String, ResultHandler), like so:
TeamResultHandler rh = new TeamResultHandler();
session.select("getTeamsByGameID", rh);
List<Team> lt = rh.getTeamList();
return lt;
This solution is more verbose than your solution (requires an extra class and three lines in your query code, rather than 2), but it only creates one List, rather than two, so you'll have to decide which fits your needs best.
In addition, ResultHandlers can be useful for additional things - making sure results are sorted in a certain way or filtered or something else, in case you need that.