Spring单个实体中的两个自动生成的ID(Two auto generated IDs in Spring single entity)
我需要在Spring中使用Annotations生成两个ID(列值)。 如果我需要使用注释,ID和UUID自动生成2个ID,那么解决方案是什么,我会使用以下生成的UUID:
@Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid2") @Column(name = "uuid", unique = true) private String uuid;
但我无法同时使用@Id注释,无法自动生成ID(第二个生成的字段)
@Id @GeneratedValue(strategy=GenerationType.AUTO) private long id;
什么是自动生成两列ID和UUID的最佳解决方案,因为只有一个@Id只创建了一个而另一个是null? 我需要一些Hibernate的解决方案,而不是以编程方式生成其中一个。
I need to generate two IDs (column values) in Spring with Annotations. What would be the solutions if I need 2 IDs auto generated with annotations, ID and UUID, I would use UUID generated with the following:
@Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid2") @Column(name = "uuid", unique = true) private String uuid;
but I would not be able to use the @Id annotation at the same time and can not auto generate the ID (second generated field)
@Id @GeneratedValue(strategy=GenerationType.AUTO) private long id;
What would be the best solution to auto generate two columns ID and UUID, since with only one @Id only one is created and the other is null? I need some solution with Hibernate, not programatically generating one of them.
原文:https://stackoverflow.com/questions/32150343
最满意答案
好的,让我们玩得开心点...
mysql> create table so (a int, b char, c char, d char, e char, f char, `key` int, dupe char); Query OK, 0 rows affected (0.05 sec) mysql> insert into so values (1, 'd', 'c', 'f', 'k', 'l', 1, 'x'), (2, 'g', null, 'h', null, 'j', 1, null), (3, 'i', null, 'h', 'u', 'u', 2, null), (4, 'u', 'r', null, null, 't', 2, 'x'); Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from so order by a; +------+------+------+------+------+------+------+------+ | a | b | c | d | e | f | key | dupe | +------+------+------+------+------+------+------+------+ | 1 | d | c | f | k | l | 1 | x | | 2 | g | NULL | h | NULL | j | 1 | NULL | | 3 | i | NULL | h | u | u | 2 | NULL | | 4 | u | r | NULL | NULL | t | 2 | x | +------+------+------+------+------+------+------+------+ 4 rows in set (0.00 sec)
Python 2.6.5 (r265:79063, Mar 26 2010, 22:43:05) [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>> db = MySQLdb.connect(host="127.0.0.1", db="test") >>> c = db.cursor() >>> c.execute("SELECT a, b, c, d, e, f, `key`, dupe FROM so") 4L >>> rows = c.fetchall() >>> rows ((1L, 'd', 'c', 'f', 'k', 'l', 1L, 'x'), (4L, 'u', 'r', None, None, 't', 2L, 'x'), (2L, 'g', None, 'h', None, 'j', 1L, None), (3L, 'i', None, 'h', 'u', 'u', 2L, None)) >>> data = dict() >>> for row in rows: ... key, isDupe = row[-2], row[-1] ... if key not in data: ... data[key] = list(row[:-1]) ... else: ... for i in range(len(row)-1): ... if data[key][i] is None or (not isDupe and row[i] is not None): ... data[key][i] = row[i] ... >>> data {1L: [2L, 'g', 'c', 'h', 'k', 'j', 1L], 2L: [3L, 'i', 'r', 'h', 'u', 'u', 2L]}
OK, let's have some fun...
mysql> create table so (a int, b char, c char, d char, e char, f char, `key` int, dupe char); Query OK, 0 rows affected (0.05 sec) mysql> insert into so values (1, 'd', 'c', 'f', 'k', 'l', 1, 'x'), (2, 'g', null, 'h', null, 'j', 1, null), (3, 'i', null, 'h', 'u', 'u', 2, null), (4, 'u', 'r', null, null, 't', 2, 'x'); Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from so order by a; +------+------+------+------+------+------+------+------+ | a | b | c | d | e | f | key | dupe | +------+------+------+------+------+------+------+------+ | 1 | d | c | f | k | l | 1 | x | | 2 | g | NULL | h | NULL | j | 1 | NULL | | 3 | i | NULL | h | u | u | 2 | NULL | | 4 | u | r | NULL | NULL | t | 2 | x | +------+------+------+------+------+------+------+------+ 4 rows in set (0.00 sec)
Python 2.6.5 (r265:79063, Mar 26 2010, 22:43:05) [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>> db = MySQLdb.connect(host="127.0.0.1", db="test") >>> c = db.cursor() >>> c.execute("SELECT a, b, c, d, e, f, `key`, dupe FROM so") 4L >>> rows = c.fetchall() >>> rows ((1L, 'd', 'c', 'f', 'k', 'l', 1L, 'x'), (4L, 'u', 'r', None, None, 't', 2L, 'x'), (2L, 'g', None, 'h', None, 'j', 1L, None), (3L, 'i', None, 'h', 'u', 'u', 2L, None)) >>> data = dict() >>> for row in rows: ... key, isDupe = row[-2], row[-1] ... if key not in data: ... data[key] = list(row[:-1]) ... else: ... for i in range(len(row)-1): ... if data[key][i] is None or (not isDupe and row[i] is not None): ... data[key][i] = row[i] ... >>> data {1L: [2L, 'g', 'c', 'h', 'k', 'j', 1L], 2L: [3L, 'i', 'r', 'h', 'u', 'u', 2L]}
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
我认为这会有效,而且效率很高: items = [['A','B'], ['B','C'], ['C','E'], ['E','F'], ['F','G']] nodes = dict(items) changed = True while changed: changed = False keys = nodes.keys() for prevEl in keys: if not prevEl in nodes: #may have been deleted ...
-
当然,有很多方法。 这是一个简单的列表理解: >>> a = [0, 2, 4] >>> b = [1, 3, 5] >>> [p for pair in zip(a,b) for p in pair] [0, 1, 2, 3, 4, 5] >>> 或者如果你更喜欢使用itertools >>> import itertools >>> list(itertools.chain.from_iterable(zip(a,b))) [0, 1, 2, 3, 4, 5] Sure, there are ma ...
-
尝试这个 select T.key1,T.key2,T.serial,T.info1,T.info2,T.date,p1.product_data,p2.product_data from (SELECT m.key1 as key1,m.key2 as key2,m.info1 as info1,m.info2 as info2 ,m.date as date,p1.serial as serial from main_info as m join product1 as p1 on m.k ...
-
由于两种情况之间的不一致,您想要的输出看起来有点奇怪。 您可以通过简单地更改此示例来获得所需的输出,但是: lists = [ [u'a', 11, u'P'], [u'a', 11, u'A'], [u'b', 2, u'P'], [u'c', 1, u'P'], [u'c', 2, u'P'], [u'd', 1, u'P'], [u'e', 3, u'P'], [u'f', 2, u'P'], [u'a', 1, u'P'], [u'a', 2, u'P'], [u'b', 1, ...
-
import itertools as it list(it.chain.from_iterable(it.izip(a,b,c))) import itertools as it list(it.chain.from_iterable(it.izip(a,b,c)))
-
好的,让我们玩得开心点... mysql> create table so (a int, b char, c char, d char, e char, f char, `key` int, dupe char); Query OK, 0 rows affected (0.05 sec) mysql> insert into so values (1, 'd', 'c', 'f', 'k', 'l', 1, 'x'), (2, 'g', null, 'h', null, 'j', 1, null), ( ...
-
在python中合并列表(to merge lists in python)[2022-11-10]
为什么要为每个要检查的文件系统生成单独的进程,而df能够在一次运行中检查所有这些进程? df1 = subprocess.Popen(['df','-h', '/tmp' , '/usr/mware' , '/var' , '/var/mware'], stdout=subprocess.PIPE).communicate()[0].split() Why do you spawn separate processes for every filesystem to check, while df is ... -
无论列表长度如何,您的代码始终会附加每个列表的最后一个元素。 相反,请尝试此扩展: tlist.extend(list2[c+1:]) tlist.extend(list1[c+1:]) 我得到了这个改变的预期输出:它从循环离开(c)的地方开始,而不是从最后一个元素(n4,n5)开始。 另外,你的while循环绑定是错误的; 它只有在两个列表长度相差不超过1时才有效。否则,你会得到一个超出范围的下标错误。 尝试这个: for c in range(min(n1, n2)): tlist.inser ...
-
Python合并列表(Python merge lists)[2023-01-14]
您可以使用以下内容: import urllib from bs4 import BeautifulSoup url = 'http://python-data.dr-chuck.net/comments_213060.html' html = urllib.urlopen(url).read() soup = BeautifulSoup(html) tags = soup('span') lst = [int(tag.text) for tag in tags] print(sum(lst)) 产量 ...