Anatoly Libman
Anatoly Libman

Reputation: 281

Jpa annotations

Is the following code legal:

@Entity
@Embeddable

public class UserProfile {

@Id
private String name;
private String pass;
..

}

Can I use both annotations at once?

Upvotes: 2

Views: 672

Answers (4)

F3rr31r4
F3rr31r4

Reputation: 268

no, se here http://www.objectdb.com//api/java/jpa how to do it

other thing, the id is automatic generated, but yes you can change as you want ;)

Upvotes: 2

Alex Vaz
Alex Vaz

Reputation: 516

Ok, we are confusing things here.

An entity is a java class for whose instances you want persistence in the db in the form of one table per entity, a row per instance.

Other instances of other classes can either be entities with their own tables and linked with the one to many, many to many, and such annotations, or they can be "embedded" directly inside the same table of another entity. This is most useful when you have complex keys and you need separate classes to define the primary keys.

So, either you have an embeddable class whose instances will be persisted inside another entity, or you have an entity that will be persisted to its own table.

Upvotes: 0

magomi
magomi

Reputation: 6685

No. Embeddables are designed to be embedded into entities. They are used to implement the composition pattern.

Upvotes: 0

Neil
Neil

Reputation: 5780

Why must there always be people who, when instructed not to press a button, will do it just to see what happens? Embeddable is a type, just like Entity and should not be used together, since by definition, an Embeddable cannot have an Id.

Upvotes: 4

Related Questions