get values from impex into InitDefaultInterceptor Hybris - java

I have an impex file(example)
INSERT_UPDATE Subscriber;firstName[unique=false];lastName[unique=false];email[unique=true];bulk(code)[default=false]
;FirstName;lastName;email#gmail.com;true;
and an InitDefaultInterceptor
public class MyInterceptor implement InitDefaultInterceptor<SubscriberModel>{
onInitDefaults(SubscriberModel model, InterceptorContext ctx)
}
How can I get values from the impex in this interceptor? I try to use
model.getFirstName();
....
but all the methods returns "null". What can I do to get the values ? I need to implement a logic before I save them into db.

If you want to make use of the values that are being sent, you need to use a PrepareInterceptor instead of a InitDefaultInterceptor interceptor.
InitDefaultInterceptor : The Init Defaults Interceptor is called when a model is filled with its default values. In your case, this happens at creation of the new instance of the object you want to add to the database. This interceptor is used to add default values (next to the ones you already defined in your items.xml). Only the defaults, marked in your items.xml are inserted at this point. No data from your impex is loaded here, as this just handles the defaults for new objects, no matter the content that will be added at a later stage.
PrepareInterceptor : The Prepare Interceptor is called before a model is saved to the database. Use this to add values to the model or modify existing ones before they are saved. In this interceptor, the values of your impex will be filled in the model object. You can add or modify your data here depending on your usecase.
For more info on all type of interceptors, there is a help page from SAP that describes all of them.

Related

How can I insert data into the database when an entity is created?

I'm creating a website for a school project which uses spring for the backend. I'm trying to insert data into the database when new data is saved to a specific table.
I've tried using #HandleAfterCreate and #PrePersist, but neither worked. I'm not very experienced with spring. The teacher told us to use it and now I don't know what do.
#HandleAfterCreate
public void handlePersonBeforeCreate(Person person){
logger.info("Inside Person Before Create....");
Set<Qualifikation> qualifikationen = new HashSet<>();
kompetenzRepository.findAll().forEach(kompetenz -> {
Qualifikation qualifikation = new Qualifikation();
qualifikation.setAusmass(0);
qualifikation.setKompetenz(kompetenz);
qualifikation.setPerson(person);
});
person.setQualifikationen(qualifikationen);
System.out.println(person.getDisplayName());
}
The code should set a person's "Qualifikation" to a default value when the person is inserted (via OAuth login). It should have every "Kompetenz" with a value of 0 by default. Kompetenz has a 1 to n relation to Qualifikation. If you need more information please ask me.
It looks like you're trying to have access to the repository layer of your application inside an entity. This is generally not a good idea, as the entities should only know about the data they hold, not the other application components.
In this particular case it would be wise to use a #Service class with a method that you can call to insert the data into the database. In the method you could then insert any other entities as well. Let your repositories be fields of the service and make them #Autowired.
I think you need to enable JPA auditing . It can be enabled in Spring by add #EnableJpaAuditing to your persistence configuration. This tells Spring to listen JPA entity lifecycle events and call the annotated methods in appropriate places.
Also I think you should make the callback method private if it is meant to be called only when persisted (#PrePersist).
See details here. In this article is also presented entity listeners which might also be a good solution when dealing with multiple entities having a need for same pre-persist functionality.
I think you should create a service class, a repository class and an entity which will be stored through repository. The logic of getting all inner elements and filling it with default value is to be written in service and not a good idea to write in entity class.
If you need any help regarding it, let me know .
Welcome to community!!

Lazy loading of the content

I have two entities : User and Post (relation one-to-many). Post fields: id, creationDate, title, content, user.
Data is stored in the database and accessed via Hibernate.
I have a controller to pass Post object as a JSON to JavaScript. Then it is shown on the web page. But it is not always necessary to pass all the Post fields. For ex., I need to show to the user only title and creation date, and if the user presses the button Show content, only then I need to show post content (which I want to request from server only when it is need to show).
So here is a problem: How can I implement lazy initialization of the content field in Post object? Should I write two methods in my controller: one for generating JSON with list of Posts and setting content field to null or empty String, and another to pass only content string?
Make post content an object and a single table in db.
It looks like the following in java:
public class Post {
...
PostContent postContent;
}
First you may try to initialize the lazy collection at the DAO via Hibernate.initialize(lazyCollection). If it didn't work then either use FetchType.EAGER or keep the session open during request and the collection should be fetched when needed.

Access Custom Field Salesforce

I have a created a custom field in Contacts object in Salesforce whose API name is "Resume_Text__c" and I'm making a SOAP call to get the value of that filed using Java Implementation by writing a following SOQL.
SELECT Resume_Text__c FROM Contact
But execution of query throwing following exception.
No such column 'Resume_Text__c' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.'
So how can I access custom field via Soap API Java Implementation?
Whenever you are using Enterprise.wsdl file in your implementation, you need to make sure that every time you create some new fields and object on Salesforce.com environment, you refresh your Enterprise.wsdl to import all the dependency mappings else go with Partner.wsdl.

How do you turn on a hibernate filter for a particular entity by default?

I currently have an Entity that has a where clause set on it. I want to put that where clause on a filter and have that turned-on by default (so that I won't break any existing functionalities).
I want to turn it into a filter so that I can disable it because I have a use case wherein I need it disabled.
How can I do that in hibernate 3.1.3?
The Hibernate documentation seems to state that Filters (akin to a SQL view) are optional ways of viewing your data and do not represent the "default" way of looking at it. My recommendation would be to stick with the where clause that you have and write a special accessor routine for the use case where you don't want filtered data.
I suppose another alternative to achieve what you're asking would be to encapsulate the enabling of your filter within your "HibernateUtil" class (assuming you have such a beast that is responsible for opening new sessions for your code to use). By default your method to obtain a new session would instantiate the session and then enable the filter before returning. Something like this:
public Session newSession(){
Session session = sessionFactory.openSession();
// this assumes you don't have criteria to set depending on the context
session.enableFilter("yourFilter");
return session;
}
Then in your specific use case you could disableFilter("yourFilter");.

What are attributes?

Could anyone please clarify the defination of attribute?
for example, in the following code, what is an attribute:
request.setAttribute("ja",new foo.Employee());
Is the attribute in the above code an object of type foo.Employee(), or it is key/value pair, or it is actually "ja"?
Request attributes are values indexed by a key (in your case "ja") which are shared in the life of the request object. In Java filter, servlet, jsp, include and forward use same request object so for example you can push an object in a servlet and pull it in a JSP.
The same approach is for session and application scopes
Request attributes are (or at least act like) a map of objects, in this case the key is "ja" and the value is a new foo.Employee.
The session, page, and application have the same data structure.
From the servlet API specification:
Attributes are objects associated with a request. Attributes may be set by the
container to express information that otherwise could not be expressed via the API,
or may be set by a servlet to communicate information to another servlet (via the
RequestDispatcher). Only one attribute value may be associated with an attribute name.
Here an attribute is a custom piece of information (here a new foo.Employee) added to your request (in a Map,Object> . This information will last as long as this request is processed and it can be used later in the process, for example by a JSP.
It's a key value pair
From the docs:
setAttribute
public void
setAttribute(java.lang.String name,
java.lang.Object o)
Stores an attribute in this request. Attributes are reset between
requests. This method is most often
used in conjunction with
RequestDispatcher.
Attribute names should follow the same conventions as package names.
Names beginning with java., javax.,
and com.sun.*, are reserved for use by
Sun Microsystems.
If the value passed in is null, the effect is the same as calling
removeAttribute(java.lang.String).

Categories

Resources