I am marking some of JPA class with predefined interface say Auditable. Now I want to invoke EntityListeners Auditor #PrePersist marked method. This has to be done to only those entity which implements this interface. I can see we can do this on Parent Classes but are interface allowed ?
Using interface is wrong here it is you must use abstract class ,interface is just used to represent class behavior not persistent field. by the way try see eclipse link they support additional thing in interface.
http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Interfaces
Related
I am new to MyBatis, I saw some code which define model as
public class model implement serializable {
****
}
but some codes simple define without serializable interface.
I am wondering which is better? Serializable is an empty interface actually.
You need to define the Serializable interface if you plan to serialize instances of your class. It's that simple.
Many do it out of routine, but the entire point of Serializable is that some classes can NOT be serialized correctly. By making you implement this interface, you make the conscious decision that your class, in fact, can be serialized.
Mybatis don't require serialization. It dynamically calls constructor after executing query and create bean objects.
So answer is no you don't need to implement Serializable interface.
Serializable is a marker interface and has no method. It just tell jvm that you are intrested to serialize the type and rest will be done automatically.
I know what I wanna do sounds stupid but listen:
I have abstract classes Entity and Player (extends Entity) and non-abstract classes TestPlayer (extends Player) and TestMob (extends Entity).
Now I am facing the following problem:
I want to implement some abstract methods in Entity with the same functionality inside TestPlayer and TestMob. Theoretically I could just create another class TestEntity (extending Entity) and make TestPlayer and TestMob inherit from it. But then TestPlayer couldn't inherit from Player anymore.
Implementing the functionality directly inside Entity is not an option as it isn't intended for all sub-classes to have this functionality.
What could be considered an acceptable solution other than having duplicate code?
You could also use the Decorator Pattern (https://en.wikipedia.org/wiki/Decorator_pattern). You have a interface Entity and two classes implementing it: ConcreteEntity and Decorator, where ConcreteEntity has the default logic and Decorator has a reference to Entity and delegates all method invocations. Then you can extend Decorator. Instead of new TestPlayer(), you have new Player(new TestEntity(new ConcreteEntity)))
Not sure if it's good practice but what if TestPlayer and TestMob both hold a reference to TestEntity instead of inheriting from it and inside every of the respective methods call the corresponding method inside TestEntity. #CompositionOverInheritance
I want to create a DAO class named BaseDAO that should have the JPA and JDBC capabilities in Spring. I mean, I want to extend JPADAOSupport and JDBCDAOSupport classes of spring in to my BaseDAO class. I am aware that multiple inheritance is not an option in Java.
I have created two separate Base classes like BaseJPADao and BaseJdbcDao extending the respective classes. Is it possible to have a single class to extend both? Is there any design pattern solving this issue. Please advise.
Why don't you have a DaoGateway bean having injected the actual JPA DAO and the JDBC DAO beans.
This gateway can then decide which DAO to delegate a given request (to JPA or to JDBC).
You should always favour composition vs inheritance when reusing functionalities.
no it is not. if it was possible, you would still have the same result as in
one class extending JPADAOSupport and JDBCDAOSupport, which you yourself say you know is not possible because multiple inheritance is impossible.
you can write to an interface, and provide two implementations, though.
This would be easy to do with delegation if they both had interface level access you want:
public class MyUberClass implements WhateverJPADAOSupportDoes, WhateverJDBCDAOSupportDoes {
private JPADAOSuport jpa;
private JDBCDAOSupport jdbc;
// now implement all methods specified by the interfaces on the class signature and delegate to their respective member
}
But it seems you want access to all of their public methods. As there is no interface for both you can do the same as above but it can't be of both types simultaneously. The language expressly denies you this.
Your only other option is to create an adapter interface that your code can rely on and then use the combination delegation. If you're hoping to have one class that you can just drop in as a substitution for both then the answer is you can't.
Suppose I had an interface with some annotation(s), for example:
#SpecialClass
public interface IFoo { /* ... */ }
And suppose I make a class that implements the interface:
public class Foo implements IFoo { /* ... */ }
Is it possible for class Foo to somehow "inherit" or automatically copy all or some of the annotations from IFoo and its members (e.g. automagically annotate Foo as #SpecialClass, etc.)?
This would be convenient for implementing web service classes (e.g. those generated by the JAX-WS "wsimport" tool) by just implementing their annotated interfaces without explicitly having to copy the interface annotations to the implementing class (e.g. javax.jws.WebService, javax.xml.ws.RequestWrapper, etc).
EDIT: I'm leaving this answer here for general information and future readers, but Andreas pointed out an important bit of the Javadoc which I'd missed:
Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class. Note also that this meta-annotation only causes annotations to be inherited from superclasses; annotations on implemented interfaces have no effect.
In other words, it wouldn't help in this situation. Also it's only useful if you have control over the annotation itself, of course.
I suspect the real answer is that you simply have to apply the annotation everywhere. If you're worried about forgetting one, you might want to write a unit test which finds all your classes (easier said than done, I realise) and checks that the annotation is present for all classes implementing the given interface.
Have you tried applying the Inherited annotation to the SpecialClass annotation itself?
Indicates that an annotation type is automatically inherited. If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class's superclass will automatically be queried for the annotation type. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached. If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation.
That certainly sounds like exactly what you want.
What are tagging interfaces and what are they used for?
A tagging interface typically has some magic associated with it: either directly built into the VM, or using reflection. Because the magic could technically apply to any class, you use the tagging to indicate that you thought well about the magic and whether it applies to your class.
Because sometimes, it really makes sense if some property of a type can be used as a type itself - Serializable comes to mind. If I make a method like this:
public void save(Object data){ ... }
... you don't really know how that data will be saved. VM serialization? Bean property serialization? Some homebrewed scheme? Whereas if you write it like this:
public void save(Serializable data){ ... }
... it is quite clear (if only the designer of ObjectOutputStream had used this possibility!). Sometimes it makes sense to use annotations when you want to add meta-data to types, but in this case, I'd argue for a tagging interface.
The question of marker interfaces vs annotations is discussed in Bloch's "Effective Java", and part of that section is available on google books here
It was used to mentioned some property of a class (like Serializable shows, that the class is allowed to serialize). Now annotations could do this job.
In addition to the other answers marker interfaces can also be used to specify additional properties of a class that is not inherited by some other already-implemented interface. One example of this would be the interface RandomAccess. It denotes a collection that can be accessed randomly without loss of performance and does not have to be accessed via an iterator to achieve that performance.
You can tag your class with a tagging interface to say to your fellow developer and consumer of your class that you explicitely support that functionality. Think of Serializable; someone who needs to persist a Session and uses serialization to do that can safely use an object of your class.
It can be further used in reflection; nowadays it is common to use annotations to do this but in the olden days you can inspect a class, check whether it implements a certain interface (like DAO) and if so, process the object further (I'm thinking about the Entity annotation here).
tagging interfaces are interfaces with no abstract methods inside , they are used to add a data type for the class which implements them and to be a parent interface for other interfaces ( especially with multiple inheritance in interfaces )
public interface name {}
public interface john1 {}
public interface john2 {}
public interface Demo extends john1 , john2 , name {}
** when JVM sees the name interface , it will find out that the Demo will exert a specific cenario .
I would also add you can use tagging interfaces to restrict ownership of an instance:
interface IFlumThing;
interface IFlooThing;
class BaseThing {...}
class FlumThing extends BaseThing implements IFlumThing {};
class FlooThing extends BaseThing implements IFlooThing {};
class Flum {
addThing(IFlumThing thing){...};
}
class Floo {
addThing(IFlooThing thing){...};
}