I am new to spring I want to get all the data whose id=given by me. Can any one tell me how can I do That
#Override
public Collection<Device> listDevice() {
Collection<Device> deviceCollection=new ArrayList<>();
Iterable<Device> deviceIterable=deviceRepository.findAll();
for(Device d:deviceIterable){
deviceCollection.add(d);
}
return deviceCollection;
}
This is giving me all the data But I want data having id
long id=device.getManufacturer_id();
please tell how to do that
Instead of Repository#findAll, use Repository#findById(ID primaryKey).
Check this documentation:
http://docs.spring.io/spring-data/jpa/docs/1.0.0.M1/reference/html/
Instead try SpringDao library for JDBC related operations in Spring framework. It gives lots of pre built classes for requirements like your like NamedParameterJdbcDaoSupport, JdbcDaoSupport etc.
Will find host of examples of that on the net. just google it.
Related
I need some of your inputs in deciding a right approach in following case.
I am currently working on a ecommerce application (An online shopping website). Here, on homepage of application, I have to display a list of products available in shop. The end user has a facility of applying filters (include/exclude) to products list so that only the products satisfying the applied filter will be displayed.
So, my question is regarding the correct design for applying these filter on products in Java layer. Please note I keep all the data in-memory (hashmap) at Java layer. I just want to apply filters that are passed as an input from UI on this in-memory data store and return back the filtered results.
Backend: Java application hosted on Tomcat. This application has a periodic thread running that reads/refreshes product data from file system every 30 seconds and keeps it in-memory of java process.
Frontend: React application hosted on Nginx. Makes rest calls to backend server to fetch the data.
Approaches I considered:
Create a class called "FilteredProducts" that has attribute (say filtering criteria). Have different implementations for each possible filtering criteria using strategy pattern and apply the filtering criteria on products based on filtering criteria that is passed as an input.
Can anyone please guide me, if there is any recommended way to handle this requirement? Any input is highly appreciated. Please let me know if more information is required in this context.
You could create an interface or abstract class filter and for each filter element you could create a specific class that implements or extends the interdace/superclass.
Then you could implement a matches method and check, which articles meet the criterias (e.g.:)
public interface FilterCriteria() {
public List<ShopElement> getMatchedProducts(int minPrice, int maxPrice);
}
public class PriceFilterCriteria implements FilterCriteria() {
#Override
public List<ShopElement> getMatchedProducts(int minPrice, int maxPrice) {
List<ShopElement> matchedProducts = new ArrayList<>();
foreach(ShopElement se : YourDataSource.getAllElements() ) {
if(se.getPrice() > minPrice && se.getPrice() < maxPrice) {
matchedProducts.add(se);
}
}
}
}
This pattern is really called filter pattern.
Your strategy pattern approach may work, but i think it is some kind of 'wrong-placed' in this context, because i think strategie pattern is mostly for changing concrete implementations within a client and not for combining various, cross-cutting filters.
I have a large edmx schema file that would be very inconvenient to manually re-create, one EntityType at a time, in Java using OLingo. While I'm not opposed to writing a loader of some kind, I wanted to make sure that OLingo 4 doesn't already provide this functionality.
I found an article that shows how OLingo 2 can load this kind of information:
#Override
public Edm readMetadata(final InputStream inputStream, final boolean validate)
throws EntityProviderException {
EdmProvider provider = new EdmxProvider().parse(inputStream, validate);
return new EdmImplProv(provider);
}
But I need to use version 4. I haven't found the same interfaces in the documentation for version 4, so I'm at a bit of a loss. Any pointers much appreciated.
After more investigation, I found that I needed the odata-server-core-ext package and I could import org.apache.olingo.server.core.MetadataParser. Among other things, this class has a function called buildEdmProvider(Reader) which does the work of building a SchemaBasedEdmProvider for you.
If you're not bound to OLingo, you could also try odata-client: https://github.com/davidmoten/odata-client
I've not had a good chance to use it myself as unfortunately the web service I'm trying to connect to is OData 2, and odata-client only supports 4. However, it looked to have some neat features (including type safety and automatic/transparent paging).
I'm doing an assignment where I have to use a web service using apache axis (Using eclips Mars) to make a desktop application in Java. It has to use an existing dynamic web project I already created.
Web project was to add/remove companies and employee details in a (Oracle) database in a web interface. It worked as required. But when the web service was created, It doesn't allow me to create a web client. It gives this error:
IWAB0399E Error in generating Java from WSDL:
java.io.IOException: ERROR: Missing <soap:fault> element inFault "IOException"
in operation "IOException", in binding getCompanies
Apparently, it wont allow me to return HashMaps from methods I created. (When I changed my whole project without returning Hashmaps, I can create the client) But I need to get HashMaps.
Are there any way to get HashMaps from the web service I created ???
I've refereed This question in SO. But I have no idea what's the accepted answer was saying.
EDIT:
OK. Now I know that I can't use HashMaps in web services as they can't be marshal and unmarshal. Then I found This question which I tried. But the problem still stands. (I guess I didn't used the answer mentioned above correctly.) As a beginner in this field, I actually don't get how to wrap (Or serialize) Hashmap and retrieve it back. Can someone show an example ?
You can try to wrap your HashMap in a class and create a custom adapter using it with #XmlJavaTypeAdapter to allow JAXB to make the object serialisation correctly.
public class Response {
#XmlJavaTypeAdapter(MapAdapter.class)
HashMap<Integer, Student> students;
public HashMap<Integer, Student> getStudents() {
return students;
}
public void setStudents(HashMap<Integer, Student> map) {
this.students = map;
}
}
Then just use this class as a return value of your web method.
See more:
Doc API
Example
I am using Objectify version 4. I want to use transactions in my project. Project is based on GWT Java and objectify. As per objectify tutorials i found that ofy().transact() method to be used. So i preferred to use the following
ofy().transact(new VoidWork() {
public void vrun() {
Here i wrote code for saving data to entity
}
});
When i execute the project on development server/local i get a error message stating that
No source code is available for type com.googlecode.objectify.VoidWork; did you forget to inherit a required module?
The method createBillingDocs() is undefined for the type new VoidWork(){}
createBillingDocs is my method which i want to execute in transaction.
So any help?
Thanks in advance
You can't run transactions or use Objectify client-side; it is a server-side framework for accessing the datastore. You need to separate out your client-side logic from your server-side logic and define your GWT modules carefully.
So I'm writing a simple application and rather then fumble my way through it I wanted to see if someone can give advice on how to set this up.
I have a wicket app, that I'm basically going to do something like a forum app (I've been looking at the wicket guestbook example) however I want to persist these messages to a database. I would like to use a listview (or maybe a repeater) to display the messages. I have created the db (mysql) and tables, and created entity objects for them.
What is a best practice for interfacing wicket with the db. I think I need a loadabledetachablemodel but I'm open to suggestions.
Should I create a class that implements the loadabledetachable interface and do my transactions to the database in that class? Should I do a DAO? and load in the data in the model by calling methods in the DAO? I've been looking at examples and I'm confused on how to do this. I am already learning wicket and the whole JPA stuff so I really don't want to throw something like Spring on this. I need to be able to add/update/delete and I'm just trying to figure out where is the best place to do this.
Any help or guidance is appreciated.
I would suggest you use simplest model like Model or PropertyModel. When rendering the page, load data from database into your model and display the data. When user press update button, read new value from model and then update to the database.
You don't need any LoadableDetachableModel right now since you're still learning the basic part of the wicket framework.
About DAO part, if you just want to focus on learning wicket, create a simple CRUD application without DAO class is okay. You can always refactor those code into DAO classes later anyway. But if you're trying to learn developing a web application, then you should create a DAO class and learn about the concept of Multiple Tier Architecture.
There is a pretty good explanation in German - but the code is in English:
The solution extends the LoadableDetachableModel to hold an EntityFacade by storing the classname and performing the lookup on load().
I think the example from here:
http://hotchpotch-blog.de/2012/10/09/wrapping-eines-ejb-in-ein-loadabledetachablemodel/
can be extended to something like this (not production ready):
public class EjbModel extends LoadableDetachableModel {
/**
* Get the name of the EJBInterface type suitable for lookup.
*
*/
protected String ejbName;
public EjbModel(EJBInterface bean){
//store the bean so it will get detached
super(bean)
ejbName = bean.getSimpleName() + "Facade"; //not sure if + "Facade" is needed
}
#Override
protected EJBInterface load() {
EJBInterface result = null;
try {
InitialContext ctx = new InitialContext();
result = (EJBInterface ) ctx.lookup("java:module/" + ejbName);
} catch (NamingException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Error at JNDI lookup for " + getEjbName(), ex);
}
return result;
}
}