Sleep mode and without PC

Is it possible to create a table (from JPA annotated Hibernate @Entity ) that does not contain the / Id primary key?

I know this is not a good idea; the table must have a primary key.

+46
java database hibernate jpa
Apr 20 '09 at 7:44
source share
9 answers

I found this impossible to do. So bad for those who work with legacy systems.

If you reverse engineer (create annotated JPA objects from an existing JDBC connection), the table will create two Java classes, one Entity and one field; id and one embeddable id containing all columns from your relation.

+10
Apr 29 '09 at 13:30
source share

Roger's answering machine is correct. To talk a little about what I mean (at first I did not understand it and thought it would help):

Let's say you have a Foo table as such:

 TABLE Foo ( bar varchar(20), bat varchar(20) ) 

You can usually write a w / Annotations class to work with this table:

 // Technically, for this example, the @Table and @Column annotations // are not needed, but don't hurt. Use them if your column names // are different than the variable names. @Entity @Table(name = "FOO") class Foo { private String bar; private String bat; @Column(name = "bar") public String getBar() { return bar; } public void setBar(String bar) { this.bar = bar; } @Column(name = "bat") public String getBat() { return bat; } public void setBat(String bat) { this.bat = bat; } } 

.. But hell. There is nothing in this table that we could use as an id, and this is an outdated database that we use to [insert an important business function]. I do not think that they will allow me to start modifying tables so that I can use hibernation.

Instead, you can split the object into a working structure with hibernation, which allows you to use the entire string as a key. (Naturally, this assumes the string is unique.)

Divide the Foo object into two:

 @Entity @Table(name = "FOO") class Foo { @Id private FooKey id; public void setId(FooKey id) { this.id = id; } public void getId() { return id; } } 

and

 @Embeddable class FooKey implements Serializable { private String bar; private String bat; @Column(name = "bar") public String getBar() { return bar; } public void setBar(String bar) { this.bar = bar; } @Column(name = "bat") public String getBat() { return bat; } public void setBat(String bat) { this.bat = bat; } 

}

.. And it should be so. Hibernate will use the Embeddable key to identify it, and you can make the call as usual:

 Query fooQuery = getSession().createQuery("from Foo"); 

We hope that this will help first graders to work with this.

+49
Nov 21 '09 at 17:29
source share

Use the following code; Hibernate does not have its own logic for distinguishing duplicate entries

Let me know if there are any problems with this approach.

 @Entity @IdClass(Foo.class) class Foo implements Serializable { @Id private String bar; @Id private String bat; public String getBar() { return bar; } public void setBar(String bar) { this.bar = bar; } public String getBat() { return bat; } public void setBat(String bat) { this.bat = bat; } } 
+37
Sep 29 '11 at 7:27
source share

I found that this trick works:

 <id column="ROWID" type="string" /> 
+6
May 19 '16 at
source share

You do not need to create a separate class for your @Id or primary key. Just use Integer (or something else). Also, do not publish the fake key, as the developers who use it may think that it is real and otherwise try to use it. Finally, this is best used in VIEW. I agree with earlier reports that in most, if not all cases, tables should have a primary key. For example:

 @Entity @Table(name = "FOO") class Foo { @SuppressWarnings("unused") @Id private Integer id; @Column(name = "REAL_COLUMN") private String realColumn; public String getRealColumn() { return realColumn; } public void setRealColumn(String realColumn) { this.realColumn= realColumn; } } 
+2
May 25 '11 at 19:26
source share

Create Pojo from a table - use the reverse engineering method that exists in eclipse. For a non-primary key table, eclipse will generate two Pojo classes.

 eclipse generated class and hbm.xml - --- Foo.java // public class Foo implements java.io.Serializable { private FooId id; public Foo() { } public Foo(FooId id) { this.id = id; } public FooId getId() { return this.id; } public void setId(FooId id) { this.id = id; } } --- FooId.java // public class FooId implements java.io.Serializable { private String bar; private String bat; public FooId() { } public FooId(String bar, String bat) { this.bar = bar; this.bat = bat; } public String getBar() { return this.bar; } public void setBar(String bar) { this.bar = bar; } public String getBat() { return this.bat; } public void setBat(String bat) { this.bat = bat; } public boolean equals(Object other) { if ((this == other)) return true; if ((other == null)) return false; if (!(other instanceof FooId)) return false; FooId castOther = (FooId) other; return ((this.getBar() == castOther.getBar()) || (this.getBar() != null && castOther.getBar() != null && this.getBar().equals( castOther.getBar()))) && ((this.getBat() == castOther.getBat()) || (this.getBat() != null && castOther.getBat() != null && this.getBat().equals( castOther.getBat()))); } public int hashCode() { int result = 17; result = 37 * result + (getBar() == null ? 0 : this.getBar().hashCode()); result = 37 * result + (getBat() == null ? 0 : this.getBat().hashCode()); return result; } } --- Foo.hbm.xml <hibernate-mapping> <class name="com.Foo" table="foo" schema="public" catalog="hibernate_poc"> <composite-id name="id" class="com.FooId"> <key-property name="bar" type="string"> <column name="bar" length="20" /> </key-property> <key-property name="bat" type="string"> <column name="bat" length="20" /> </key-property> </composite-id> </class> </hibernate-mapping> --- entry in the Hibernate.cfg.xml - <mapping class="com.poc.Foo" resource="Foo.hbm.xml"/> --- Fetch the Data from table - FooDataFetch.java // import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class FooDataFetch { private static Session session = null; public static void main(String[] args) { try{ Configuration cfg = new Configuration(); cfg.configure("/hibernate.cfg.xml"); SessionFactory sf = cfg.buildSessionFactory(); session = sf.openSession(); session.beginTransaction(); queryPerson(session); session.close(); }catch (Throwable ex) { System.err.println("Failed to create sessionFactory object." + ex); ex.printStackTrace(); throw new ExceptionInInitializerError(ex); } } private static void queryPerson(Session session) { Query query = session.createQuery("from Foo"); List <Foo>list = query.list(); java.util.Iterator<Foo> iter = list.iterator(); while (iter.hasNext()) { Foo foo = iter.next(); System.out.println("Foo Details: \"" + foo.getId().getBar() +"\", " + foo.getId().getBat()); } } } 
+1
Jul 21 '14 at 11:08
source share

Adding to the comment Awied. If you want to find a panel, use the following HQL.

 Query fooQuery = getSession().createQuery("from Foo.id.bar = '<barName>'"); 
+1
Mar 26 '15 at 14:29
source share

When it comes to views, instead of looking for workarounds in Hibernate, it might be easier to add a dummy identifier in your database view. I wrote about this in another question: stack overflow

+1
May 18 '17 at 14:17
source share

I found a solution for tables without a primary key and null as values. It will be powered by Oracle. Perhaps something similar exists for other databases.

  • You must create a new primary key in the POJO class:

    @Id @column (name = "identifier") private Integer id;

and use createNativeQuery like this

 getEntityManager().createNativeQuery("select rownum as id, ..... 

Your own request will generate a primary key, and you will get unique results.

0
Sep 08 '16 at 6:44
source share



All Articles