1

mI've this entity class:

@Entity  
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @ManyToOne
    private User myFriend;
    @OneToMany(mappedBy="friend")
    private Collection<User> friends;

    // Getters, Setters, serialVersionUID, etc... }
}

and this snippet to populate database (reference: JPA: How to have one-to-many relation of the same Entity type)

public static void main(String[] args) {

    EntityManager em = ... // from EntityManagerFactory, injection, etc.

    em.getTransaction().begin();

    User a   = new User();
    User b   = new User();
    User c   = new User();

    b.setMyfriend(a);
    c.setMyfriend(a);
    a.setFriends(Arrays.asList(a, b));

    em.persist(a);
    em.persist(b);
    em.persist(c);

    em.getTransaction().commit();
}

But I've problem with SQL source. Can I create a SQL source from this entity class?

Community
  • 1
  • 1
CeccoCQ
  • 3,746
  • 12
  • 50
  • 81
  • Obviously depends which JPA implementation you're using (you don't specify this in your question), and all implementations I know of provide a mechanism for DDL generation. DataNucleus JPA certainly does. – DataNucleus Mar 23 '12 at 11:27

3 Answers3

0

If your question is how to you automatically create the tables in SQL you should try adding this in your persistance.xml:

<property name="hibernate.hbm2ddl.auto" value="update" />
barsju
  • 4,408
  • 1
  • 19
  • 24
  • No, I would get the sql script. – CeccoCQ Mar 23 '12 at 09:54
  • Ok. So you don't want them auto created? You want to do it manually? What do you have so far? Can you update the post with what you have so far? – barsju Mar 23 '12 at 10:29
  • If you have some way of starting up with creating them automatically with the persistance.xml then you can go in the database and use "show create table User" to get the declaration and copy that into your script.. – barsju Mar 23 '12 at 10:31
  • I've read a old post for this purpose (attached on my post as reference). But I would know how to create a SQL table to use this entity. – CeccoCQ Mar 23 '12 at 10:32
  • I see, but the whole purpose of using JPA is so you don't have to design and create the SQL tables manually. I have a hard time understanding why you want to... – barsju Mar 23 '12 at 10:35
  • Ok, you're right. But to map an entity to table I should have first a table created into my database. So, I would only know as I can create a table that identify this entity. – CeccoCQ Mar 23 '12 at 10:37
  • No, hibernate will create the table(s) for you automatically. – barsju Mar 23 '12 at 10:39
  • Ok, I've understand. But my scope is a bit different. I would know the sql script associated to this entity because I've tried to create automatically my entities but result was different. – CeccoCQ Mar 23 '12 at 10:42
0

This is what hibernate created for me when adding your code:

CREATE TABLE `User` (
  `id` bigint(20) NOT NULL auto_increment,
  `myFriend_id` bigint(20) default NULL,
  PRIMARY KEY  (`id`),
  KEY `FKBF806F9DAD752252` (`myFriend_id`),
  CONSTRAINT `FKBF806F9DAD752252` FOREIGN KEY (`myFriend_id`) REFERENCES `User` (`id`)

Note that you have a mistake in your Entity. The mapped by value should match the attribute name (myFriend):

@Entity  
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @ManyToOne
    private User myFriend;
    @OneToMany(mappedBy="myFriend")
    private Collection<User> friends;

    // Getters, Setters, serialVersionUID, etc... }
}
barsju
  • 4,408
  • 1
  • 19
  • 24
  • I would to create these tables ( http://stackoverflow.com/questions/1831186/many-to-many-on-the-same-table-with-additional-columns), how I can do? – CeccoCQ Mar 29 '12 at 10:26
0

JPA specification doesn't require to provide a tool for exporting database schema.

However, if you use hibernate then you can use classes in http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/tool/hbm2ddl/package-summary.html package. Especially SchemaExport and SchemaUpdate.

František Hartman
  • 14,436
  • 2
  • 40
  • 60